Does a function like isset() exist in Python?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Thu Jun 23 06:32:46 EDT 2005


On Wed, 22 Jun 2005 23:09:57 -0400, Patrick Fitzsimmons wrote:

> 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.

What would you use such a function for?

The closest thing I can think of is testing whether a particular object
exists. Eg to test whether your program is running under a version of
Python that defines bools, and if not, define your own objects which act
in a similar way, you would say

try:
    False
except NameError:
    False = 0
    True = not False

Note that there is no need to do something with the name "False" in the
try clause -- just use it.


-- 
Steven.




More information about the Python-list mailing list