variables exist

Fredrik Lundh fredrik at pythonware.com
Mon Apr 11 10:14:59 EDT 2005


"fabian" wrote:

> how testing if a variable exists in python as isset in php??

try:
    variable
except NameError:
    print "variable not set"

but that is really lousy Python; better make sure you always assign to
all variables, and use None (or another suitable value) to mark that some
variable has no meaningful content right now.

that is, instead of

    if condition:
        variable = 1

    ...

    try:
        variable
    except NameError:
        ....

write

    variable = None

    if condition:
        variable = 1

    ...

    if variable is None:
        print "variable not set"

</F> 






More information about the Python-list mailing list