How can I tell if variable is defined

B

Brown, Rodrick

How could I do the following check in Python

In Perl I could do something like if ((defined($a)) { ... }

I tried if var is not None:
However this doesn't seem to work as described.

Thanks.
 
S

Sean DiZazzo

How could I do the following check in Python

In Perl I could do something like if ((defined($a)) { ... }

I tried if var is not None:
However this doesn't seem to work as described.

Thanks.

try:
var
except NameError:
handle_it()

~Sean
 
G

Grant Edwards

How could I do the following check in Python

In Perl I could do something like if ((defined($a)) { ... }

try:
yourNameHere
except NameError:
print "undefined"
else:
print "defined"
I tried if var is not None:
However this doesn't seem to work as described.

As described where?
 
M

Mel

Grant said:
try:
yourNameHere
except NameError:
print "undefined"
else:
print "defined"

This being Python, however, it can be fooled:

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information..... def __repr__ (self):
.... global b
.... return b
.... .... a
.... except NameError:
.... print "`a` is not defined"
....
`a` is not defined




Mel.
 
P

Peter Otten

How could I do the following check in Python

In Perl I could do something like if ((defined($a)) { ... }

I tried if var is not None:
However this doesn't seem to work as described.

But in Python this often is the most idiomatic way to check whether a
variable was set explicitly. Example:
.... if a is None:
.... print "using default"
.... else:
.... print "using a =", a
....using a = 42

If you tell us what you are actually trying to do we can probably come up
with a solution that is better than the literal answer

try:
a
except NameError:
a_is_defined = False
else:
a_is_defined = True

Peter
 
P

Peter Otten

Mel said:
Grant said:
try:
yourNameHere
except NameError:
print "undefined"
else:
print "defined"

This being Python, however, it can be fooled:

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.... def __repr__ (self):
... global b

Superfluous global statement.
... return b
...
... a
... except NameError:
... print "`a` is not defined"
...
`a` is not defined

This is an artifact of the interactive interpreter, like the following way
to print 42:
....
42
42
42

Peter
 
M

Mel

Peter said:
[ ... ]
This is an artifact of the interactive interpreter,

True. You can avoid the artifact by wrapping the test in a function:

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information..... def __repr__ (self):
.... return b
.... .... try:
.... obj
.... except NameError:
.... return False
.... return True
.... .... print "`a` is defined"
.... else:
.... print "`a` is not defined"
....
`a` is defined.... print "`b` is defined"
.... else:
.... print "`b` is not defined"
....
Traceback (most recent call last):

At the cost of it not quite working when the function is called with an
undefined name. I suppose the print statements could be crafted to make it
look better.


Mel.
 
D

Dave Angel

Mel said:
This is an artifact of the interactive interpreter,

True. You can avoid the artifact by wrapping the test in a function:

Python 2.6.2 (release26-maint, Apr 19 2009, 01:56:41)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
... def __repr__ (self):
... return b
...
... try:
... obj
... except NameError:
... return False
... return True
...
... print "`a` is defined"
... else:
... print "`a` is not defined"
...
`a` is defined
... print "`b` is defined"
... else:
... print "`b` is not defined"
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined


At the cost of it not quite working when the function is called with an
undefined name. I suppose the print statements could be crafted to make it
look better.


Mel.
No clue what you're trying to accomplish here. The exception inside the
function is_defined() will never fire; the method always returns True.

You're getting an exception during the attempt to call such a function,
and so of course there's no point in putting the if test around that either.

This may be the last time I try to read such complexity in an
interactive transcript. It took a couple of tries before I got the
indentation understood. I thought that is_defined() was intended to be
a method inside the class.

DaveA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top