456
votes

How do you know whether a variable has been set at a particular place in the code at runtime? This is not always obvious because (1) the variable could be conditionally set, and (2) the variable could be conditionally deleted. I'm looking for something like defined() in Perl or isset() in PHP or defined? in Ruby.

if condition:
    a = 42

# is "a" defined here?

if other_condition:
    del a

# is "a" defined here?
6
Please, please, please, do not "conditionally" set a variable. That's just dreadful, horrible design. Always, always, always provide for all logic paths. Please do not continue doing this. - S.Lott
+1 to S.Lott anwser. The fact that there is a way to do it doesn't mean you should use it in a fresh project. - e-satis
@S.Lott: I have to disagree. When I use import to "source" a "config file" (i.e. a file that only has assignments in it), it may very well be that some variable has not been defined there. And import is sure cheaper/simpler than ConfigParser, so for me pragmatism wins over beauty here. - 0xC0000022L
@STATUS_ACCESS_DENIED: That's what defaults are appropriate. First set the defaults. Then import your configuration. Now all variables are defined, either as defaults or overrides in the configuration file. You can easily avoid conditionally setting a variable. - S.Lott

6 Answers

692
votes
try:
    thevariable
except NameError:
    print("well, it WASN'T defined after all!")
else:
    print("sure, it was defined.")
399
votes

'a' in vars() or 'a' in globals()

if you want to be pedantic, you can check the builtins too
'a' in vars(__builtins__)

143
votes

I think it's better to avoid the situation. It's cleaner and clearer to write:

a = None
if condition:
    a = 42
18
votes
try:
    a # does a exist in the current namespace
except NameError:
    a = 10 # nope
5
votes

For this particular case it's better to do a = None instead of del a. This will decrement reference count to object a was (if any) assigned to and won't fail when a is not defined. Note, that del statement doesn't call destructor of an object directly, but unbind it from variable. Destructor of object is called when reference count became zero.

5
votes

One possible situation where this might be needed:

If you are using finally block to close connections but in the try block, the program exits with sys.exit() before the connection is defined. In this case, the finally block will be called and the connection closing statement will fail since no connection was created.