473,396 Members | 1,797 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 to see if variable exists

I need an if statement to test if a variable exists in a pyton script

eg. if var1 exists:
do this
else:
do this
Jul 18 '05 #1
3 66615
>>>>> "lamar" == lamar air <la*******@hotmail.com> writes:

lamar> I need an if statement to test if a variable exists in a
lamar> pyton script eg. if var1 exists: do this else: do this --
try: x
except NameError:
# x doesn't exist, do something
else:
# x exists, do something else

Jul 18 '05 #2
lamar_air wrote:
I need an if statement to test if a variable exists in a pyton script

eg. if var1 exists:
do this
else:
do this

If it's in a dictionary you can do this: var1.get('key','not found')

( just learned that all of 2 minutes ago ;] )
Jul 18 '05 #3
[Graham Breed]
lamar_air wrote:
I need an if statement to test if a variable exists in a pyton script
locals().has_key('var1')


The above could also be written:

'var1' in locals()

However, whenever I need to test this (the need is rather unusual), instead
of `if', I prefer writing:

try:
var1
except NameError:
pass # `var1' does not exist
else:
pass # `var1' exists

--
François Pinard http://www.iro.umontreal.ca/~pinard

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Ivan Marsh | last post by:
Hey Folks, Anyone know how to test for the existance of a variable in javascript? I'm looking for the equivalent of IsSet() from PHP. I'm having trouble finding it. thx --
1
by: wg | last post by:
Hey, Can someone show my how to change the code below to check to see if a variable exists rather than if the variable contains an empty string. if (document.form.regionselection.value...
2
by: Lauchlan M | last post by:
I'm retrieving url variable information uses the Request.QueryString object, eg: if (Request.QueryString.HasKeys()) key { lblMyResult.Text = Request.QueryString; } else {
0
by: Hazzard | last post by:
I am trying to figure out how to add an additional value to a querystring collection so that when I click on a datagrid cell, there will be a key to distinguish it from another column's...
3
by: Shapper | last post by:
Hello, How to determine if a Session Variable exists? In Page_Load I need to check is Session("MyVar") Exists. If it doesn't exist then I created it and give it a value: Session("myVar") =...
3
by: Adam J Knight | last post by:
Hi all, I am trying to test to see if a Session variable exists. This was my initial attempt, and doesn't work int intInstructorID = (Session.ToString() == null ?...
3
by: Mike | last post by:
Hello, How do I check to see if a GET varaible exists? This is giving me an error message. if (isset !($_GET)) {die('You must Have a Key to get in'); } Thanks
3
by: zensunni | last post by:
In a perfect world, I wouldn't have to do this, but I'm at the mercy of event scripting for a program. there's an event that runs every so often that triggers an subroutine. I'd like to declare a...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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...
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.