Testing for a Variable

Brian Quinlan BrianQ at ActiveState.com
Thu May 3 18:45:47 EDT 2001


> Hi;
> How does one test to see if a variable is defined?
> TIA,
> BenO

You could check to see if the variable is in the dictionaries returned by
vars() and globals() but that won't always work. Probably the easiest way to
check is to do something like this:

try:
	eval(var)
	# Do something
except NameError:
	# Do something else





More information about the Python-list mailing list