Does a function like isset() exist in Python?

John Roth newsgroups at jhrothjr.com
Wed Jun 22 23:30:03 EDT 2005


I'm not sure what you mean by initialized. If you're asking
if the identifier exists in the namespace, then you can use
hasattr(), or simply try to reference it and catch the exception
if it doesn't exist.

If the identifier exists, it always has a value.

On the other hand, there is a small gotcha on identifiers
in functions/methods where they have to have a value
assigned before you can reference them. If you run into
this at all frequently, you're probably making your methods
too big to be easily understood.

Of course, if you're playing games with the stack and
trying to print out the values of identifiers on the calling
chain on an exception, all bets are off. See the code in
the py.test module (part of the PyPy project) for how
you can do this.

John Roth

"Patrick Fitzsimmons" <patfitz at gmail.com> wrote in message 
news:mailman.784.1119496200.10512.python-list at python.org...
Hi,

I'm sure I should know this, but I can't find it in the manual.

Is there a function in Python like the function in PHP isset()?  It
should take a variable name and return True or False depending on
whether the variable is initialized.

Thanks for any help,
Patrick 




More information about the Python-list mailing list