Test the existence of a variable?

Dan Gass dan.gass at gmail.com
Fri Sep 3 18:41:41 CEST 2004

"Thierry S." <thierry.s at free.fr> wrote in message news:<ch9hhf$4re$1 at s5.feed.news.oleane.net>...
> Hello.
> I would to test the existence of a variable before to use it (like
> isset($myVar) in PHP).
> I try using "if myVar: ", but there is the error meesage (naturally):
> "NameError: name 'myVar' is not defined"
> Please, could you tell me what for function exist to test the variable with
> Python?
> Regards,

You may want to look at using a try/except block, for example:

    print myVar
except NameError:
    print "myVar does not exist"


More information about the Python-list mailing list