Checking for an "undefined" variable - newbie question
Dan Rawson
daniel.rawson.take!this!out! at asml.nl
Wed Aug 6 10:13:30 EDT 2003
Steven Taschuk wrote:
> Quoth Dan Rawson:
>
>>How do I check if a variable has been defined??
>
> [...]
>
>>try:
>> variable
>>except NameError:
>> ...
>
>
> This is the answer to your question.
>
>
>>If there's a more elegant way to check this, that would be great!
>
>
> Why do you want to check it in the first place?
>
> In particular, why not initialize your variable to some
> out-of-band value (such as None) and test that? E.g.,
>
> myvar = None
> # ... code which might set myvar to something else ...
> if myvar is None:
> # ...
>
So I guess it's as much a philosophy issue as a technical one <g>.
Thanks to everyone for their suggestions; I now have the script up and running!
Dan
More information about the Python-list
mailing list