variable update

Fredrik Lundh fredrik at pythonware.com
Tue Sep 12 09:02:10 EDT 2006


km wrote:

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

access it, and catch the NameError:

    try:
        variable
    except NameError:
        print "not defined"
    else:
        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"

</F>




More information about the Python-list mailing list