how can i check if a variable is set?

Chris Liechti cliechti at gmx.net
Tue Feb 5 15:47:29 EST 2002


"maximilianscherr" <MaximilianScherr at T-Online.de> wrote in 
news:mailman.1012938670.4213.python-list at python.org:

> how cna i check if a variable is set?
> like:
> 
> if variable a doesn't exist then set it to 0
> if it exists do nothing...

try to use it and handle the name NameError exception if it doesn't 
exists. or try has_attr() on class objects. or use x.__dict__.get
(varnamestr,default) [vars().get('a',0) for globals].

what best depends on your particular problem.

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list