Test the existence of a variable?

T

Thierry S.

Hello.

I would to test the existence of a variable before to use it (like
isset($myVar) in PHP).
I try using "if myVar: ", but there is the error meesage (naturally):
"NameError: name 'myVar' is not defined"

Please, could you tell me what for function exist to test the variable with
Python?

Regards,
 
R

Robin Becker

Thierry said:
Hello.

I would to test the existence of a variable before to use it (like
isset($myVar) in PHP).
I try using "if myVar: ", but there is the error meesage (naturally):
"NameError: name 'myVar' is not defined"

Please, could you tell me what for function exist to test the variable with
Python?

Regards,
if globals().has_key('myVar'):
....
 
B

Bernd Kaiser

Robin said:
if globals().has_key('myVar'):
....

I was once told, that in Python you do something and live with the
consequences.

try:
print var
except NameError, e:
print e
 
J

Jp Calderone

Robin said:
if globals().has_key('myVar'):
....
.... if globals().has_key('x'):
.... print 'x is', x
.... else:
.... print 'x is not defined'
....x is not defined

Before anyone points out locals(), I know all about it.

Testing for the existence of a variable is something to avoid doing.
Variables should be bound unconditionally. There is always a sentinel
value that can be used as a place-holder.

Instead of:

# ...
if not isset('x'):
# initialize x
# use x

use:

x = None
# ...
if x is None:
# initialize x
# use x

If None is a valid value for x to take on, pick another sentinel.
Even better is to pick a legitimate and useful value and just write the
whole thing as:

x = default
# use x

Often this is not possible... but often it is.

If it is really necessary to check for the existence of a variable by
name, for example if the variable name taken from user input, then you
may actually wish to use a dict, instead of variables in your local scope:

d = {}
# Get user input, store it in x
if x not in d:
d[x] = initialize()
# use d[x]

Jp


Jp
 
P

Peter Hansen

Bernd said:
I was once told, that in Python you do something and live with the
consequences.

try:
print var
except NameError, e:
print e

Much easier to live with the consequences if there are none:

try:
var
except NameError, e:
print e

In other words, merely evaluate the name and catch the exception,
rather than trying to print it!

-Peter
 
D

Dan Gass

Thierry S. said:
Hello.

I would to test the existence of a variable before to use it (like
isset($myVar) in PHP).
I try using "if myVar: ", but there is the error meesage (naturally):
"NameError: name 'myVar' is not defined"

Please, could you tell me what for function exist to test the variable with
Python?

Regards,

You may want to look at using a try/except block, for example:

try:
print myVar
except NameError:
print "myVar does not exist"

</dan>
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top