Checking for an "undefined" variable - newbie question

Dan Rawson daniel.rawson.take!this!out! at asml.nl
Wed Aug 6 08:00:39 EDT 2003


Duncan Booth wrote:
> Dan Rawson <daniel.rawson.take!this!out!@asml.nl> wrote in
> news:bgqmce$qk3d1$1 at ID-122008.news.uni-berlin.de: 
> 
> 
>>I have only one (ugly) solution:
>>
>>try:
>>     variable
>>except NameError:
>>     ...
>>
>>which works, but is a bit clumsy if I just want to know if the thing
>>already exists. 
> 
> 
> You could check whether the name of the variable is present in 'locals', 
> 'globals', or 'vars' as appropriate:
> 
> 
>>>>'variable' in vars()
> 
> 0
> 
Ahhhh . . . .  thanks!  'locals' and 'globals' I think I understand, but what's the scope for 'vars' ??

In the specific case in question, these are global variables, so it's relatively easy . . . .

TIA . . .

Dan





More information about the Python-list mailing list