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

Test the existence of a variable?

P: n/a
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,
--
Thierry S.
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Thierry S. wrote:
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'):
....

--
Robin Becker
Jul 18 '05 #2

P: n/a
Robin Becker wrote:
Thierry S. wrote:
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'):
....


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

try:
print var
except NameError, e:
print e
Jul 18 '05 #3

P: n/a
Robin Becker wrote:
Thierry S. wrote:
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'):
....


def f(x): .... if globals().has_key('x'):
.... print 'x is', x
.... else:
.... print 'x is not defined'
.... f(10)

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
Jul 18 '05 #4

P: n/a
Bernd Kaiser wrote:
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
Jul 18 '05 #5

P: n/a
"Thierry S." <th*******@free.fr> wrote in message news:<ch**********@s5.feed.news.oleane.net>...
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>
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.