variable update

Fredrik Lundh fredrik at
Tue Sep 12 15:02:10 CEST 2006

km wrote:

> Is there any handy untility for checking if  a variable is populated at 
> runtime ?

access it, and catch the NameError:

    except NameError:
        print "not defined"
        print "defined"

leaving variables undefined is usually bad style, though; if you can, 
assign some value to it, and test for that value instead:

    variable = None

    ... lots of code that may assign to variable ...

    if variable is not None:
        print "not defined"


More information about the Python-list mailing list