Test the existence of a variable?

Bernd Kaiser meldron at meldron.org
Fri Sep 3 07:34:34 EDT 2004


Robin Becker wrote:

> Thierry S. wrote:
> 
>> 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,
> if globals().has_key('myVar'):
>      ....
> 

I was once told, that in Python you do something and live with the 
consequences.

try:
     print var
except NameError, e:
     print e



More information about the Python-list mailing list