By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,814 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,814 IT Pros & Developers. It's quick & easy.

Does a function like isset() exist in Python?

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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" <pa*****@gmail.com> wrote in message
news:ma**************************************@pyth on.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

Jul 19 '05 #2

P: n/a
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.

Thanks for any help,
Patrick


try:
doDomething(myVar)
except NameError:
print "myVar is not set"

/Esben
Jul 19 '05 #3

P: n/a
"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.

Thanks for any help,
Patrick


There are no unitialized variables in python; if you try to access an
undefined name, a NameError exception is raised:

try:
print "foo is", foo
except NameError:
print "foo is undefined"
To undefine a defined name, use del:
foo=None
print foo None del foo
print foo

NameError: name 'foo' is not defined
George

Jul 19 '05 #4

P: n/a
George Sakkis wrote:
There are no unitialized variables in python; if you try to access an
undefined name, a NameError exception is raised:

try:
print "foo is", foo
except NameError:
print "foo is undefined"


note the order of evaluation:
try: ... print "foo is", foo
... except NameError:
... print "foo is undefined"
...
foo is foo is undefined


</F>

Jul 19 '05 #5

P: n/a
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.

Jul 19 '05 #6

P: n/a
In article <ma**************************************@python.o rg>,
Patrick Fitzsimmons <pa*****@gmail.com> 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.

Thanks for any help,
Patrick


The straight-forward thing would be to simply access the variable and catch
any resulting NameError exception that's raised if it's not defined:

try:
x
print "x is defined"
except NameError:
print "no it's not"

Next question, why do you want to do this? I suspect for most idioms where
you would something like this, the more pythonic way would be to set the
variable to None at some point, then test to see if it's still None later
on:

x = None
while foo:
if blah:
x = baz

if x != None:
print "x was assigned a value"
Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.