SunQuest
           Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old April 25th, 2002, 07:44 AM
cresswelln cresswelln is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Location: London, England
Posts: 14 cresswelln User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Testing the existnace of variable

Hi,

I have a script in which I'd like to test whether or not a particular variable has been defined or not.

The php equivalent would be something like this:

if !$MyVariable
{$MyVariable = "Hello World";}

else
{echo $MyVariable;}

What is the equivalent syntax in Python? Can't seem to find anything in any of the documentation I have.

Thanks, NICK

Reply With Quote
  #2  
Old April 26th, 2002, 12:03 AM
hasan hasan is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2002
Location: California
Posts: 0 hasan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Testing the existence of a variable

Hello,
You need to assign variables in Python before you can use them. This is
good programming practice. You can re-define variables later eg:

#!/usr/bin/env python
def main():
testVar=None
if (not testVar):
testVar = "Hello World"
print testVar
testVar = 3
print testVar
if __name__ == "__main__":
main()

prints:
Hello World
3

But, I strongly advise you to try to avoid doing this. It makes code difficult to understand and maintain. I suggest you initialise your variables before use.

Hope that helps,
Adil

Reply With Quote
  #3  
Old April 26th, 2002, 03:13 AM
cresswelln cresswelln is offline
Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2001
Location: London, England
Posts: 14 cresswelln User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks

Thanks for that

NICK

Reply With Quote
  #4  
Old June 6th, 2002, 04:18 PM
parker parker is offline
Code Poet
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Boston
Posts: 9 parker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to parker
To answer your question directly...

One way to test the existence of a variable would be:

Code:
try:
    MyVariable
except NameError:
    MyVariable = "Hello World"
print MyVariable


Best,
parker

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Testing the existnace of variable


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway