Check undefined variable
John Hunter
jdhunter at ace.bsd.uchicago.edu
Tue Jul 30 11:22:52 EDT 2002
>>>>> "Michael" == Michael Grabietz <michael.grabietz at img-online.de> writes:
Michael> Hi, how is it possible to check whether a variable 'a' is
Michael> undefined or not.
The namespace is stored in a dictionary that maps variable names to
values. You can access the namespace with the functions globals() and
locals(). Since these return a dictionary, you can use the method
has_key() to see if the name exists
>>> globals().has_key('a')
0
>>> locals().has_key('a')
0
>>> a = 1
>>> globals().has_key('a')
1
>>> locals().has_key('a')
1
John Hunter
More information about the Python-list
mailing list