[Tutor] Test if name is defined?

Sean 'Shaleh' Perry shalehperry@attbi.com
Tue Feb 11 00:53:02 2003


On Monday 10 February 2003 21:26, Terry Carroll wrote:
> Is there a way to test to see if a variable is defined other than with =
the
> exception system?
>

In a recent enough python:

def defined(thing):
    return (thing in locals()) or (thing in globals())


or, old way:

def defined(thing):
    return (locals().has_key(thing)) or (globals().has_key(thing))