473,396 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Test the existence of a variable?

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
5 3351
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
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tesla | last post by:
Hey guys, I want to PHP to do stuff only if a sertain form variable exists. How do i check to see if a variable exists or not? Something like If $http_post_vars exists, then do this. syntax...
7
by: Pietro | last post by:
Hi at all, I am looking for a mean to test if a function work with a certain Browser or not. I'ld like to make a funcrion that return true if the browse is compatible with a certain funcrion or...
14
by: Matt | last post by:
Hello, I see other references in this newsgroup saying that the only standard C++ way to test for file existence is some variant of my code below; can someone please confirm...or offer...
15
by: deko | last post by:
What I'd like to do is create an array of values and test for existence of those values. Here's the non-working code I'm having trouble with: Dim wcSearch(4 To 7) As Integer If Me.Value =...
12
by: DC Gringo | last post by:
How do I test for existence of a file in the file system: If FileExists(myVariable & ".pdf") = True pnlMyPanel.Visible = True End If -- _____ DC G
6
by: Vmusic | last post by:
Hi, I am using Javascript to add rows to tables, etc. in a function I am calling. I pass the function the ID of the div, and what I want in the rows, and it will add rows to a table in the div. ...
9
by: wildernesscat | last post by:
Hello there, I'm looking for a method to test, whether an object has a certain property. Consider the following snippet: class A { var $aaa; } $var = new A; (Assuming that the structure...
9
by: dave_140390 | last post by:
Hi, Is there a clean way to determine at compile-time whether your compiler supports the __func__ feature? I mean, something similar to the following (which tests for the existence of macro...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.