473,836 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3388
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_k ey('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_k ey('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_k ey('myVar'):
....


def f(x): .... if globals().has_k ey('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.new s.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
7924
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 please? :)
7
2567
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 style and false if not. Have you any idea? Thank in advance an dbest regards. Pietro.
14
7710
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 alternatives? Additionally, might there be cross-platform alternatives, say in a library like Boost, or something else? -Matt
15
51683
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 = wcSearch Then Do Something Else Do Something else
12
1815
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
3127
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. The problem is I need to test for the existence of the table - and if the variable or object doesn't exist already my code errors - PLEASE REMEMBER - I don't know the name of the variable or object I am testing the existance for - it is created...
9
1573
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 of class A is unknown) I need a way to
9
4132
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 __LINE__): #ifdef __LINE__ (code that uses __LINE__)
0
9810
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10819
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10526
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10567
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7771
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6972
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5641
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4437
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.