How to know when a variable is set
Justin Dubs
jtdubs at eos.ncsu.edu
Wed Nov 14 10:34:11 EST 2001
"-" <madsurfer2000 at hotmail.com> wrote in message
news:fef0a228.0111140715.24b5cfc7 at posting.google.com...
> How can I test if a variable has been given a value, or if it's undefined?
I'm a bit of a newbie, but offhand:
>>> dir()
['__builtins__', '__doc__', '__name__']
>>> foo = 5
>>> dir()
['__builtins__', '__doc__', '__name__', 'foo']
>>> 'foo' in dir()
1
>>> 'bar' in dir()
0
>>> if 'foo' in dir():
... print "foo exists"
... else:
... print "foo doesn't exist"
...
foo exists
>>> print ["foo doesn't exist", "foo exists"]['foo' in dir()]
foo exists
>>> print ["bar doesn't exist", "bar exists"]['bar' in dir()]
bar doesn't exist
>>> bar = 0
>>> print ["bar doesn't exist", "bar exists"]['bar' in dir()]
bar exists
Hehe. That was kinda fun. Well, you get the idea. There may be a much
better way, but, like I said, I'm really new to Python. I liked that one
though. :-). Have a good day,
Justin Dubs
More information about the Python-list
mailing list