How to check the exists of a name?

Piet van Oostrum piet at cs.uu.nl
Mon Oct 19 11:08:02 CEST 2009


>>>>> David <71david at libero.it> (D) wrote:

>D> Il Sat, 17 Oct 2009 23:43:36 -0700 (PDT), StarWing ha scritto:
>>> I got a idea, use a try...except statement. there are another way to
>>> do it ?
>>> 
>>> (I just curious now, because I solve my problem in another way :-)

>D> locals().has_key(myname) 
>D> globals().has_key(myname)

1. This excludes builtins. The OP says: `no matter where is it', so that
   suggests also builins.
2. The more modern invocation would be "myname in locals()" or "myname
   in globals()". In Python3 the other one isn't supported anymore.
-- 
Piet van Oostrum <piet at vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]



More information about the Python-list mailing list