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 July 28th, 2003, 02:02 AM
TheBlackMamba TheBlackMamba is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 25 TheBlackMamba User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to TheBlackMamba Send a message via Yahoo to TheBlackMamba
Checking var existance

Does any1 know how to check if a variable exists? I need this for OOP script to check if an obj's __init__ has been called. Many Thanx.

Reply With Quote
  #2  
Old July 28th, 2003, 04:33 AM
Tio Tio is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 5 Tio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi TheBlackMamba,
you can use the locals() or the vars(<parentObject>) function.
They give to you the dictionary of the currently running variables in the local or "parentObject" enviorment.
Than you can loop on it checking for isinstance(<var>,<parentClass>) if is true.

Code:
 tmp = None
 for tmp in locals():
 	if isinstance(eval(tmp),myclass):
 		print "instance founded\r\n"


HTH
Marco Bettio

Last edited by Tio : July 28th, 2003 at 04:45 AM.

Reply With Quote
  #3  
Old July 28th, 2003, 07:00 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Hi Tio,

Couldnt get the vars(locals()) to work. any idea what i'm doing wrong? I didn't try your other example it seem's like a bit of an over kill to check if a var exists and only works on a class which is a shame.

Anyway a simpler alternative to this would just be..

Code:
var = 'this is just a tring var'
if var:
    print 'var exists' 


Have fun.
Mark.

Reply With Quote
  #4  
Old July 28th, 2003, 07:35 AM
Tio Tio is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 5 Tio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi netytan,
Code:
locals()

without parameters gives you the dict with all local varables.

Code:
class a(object):
        pass

b = a()
vars(b)

gives you the dict of all variables in the scope of the created object b

with the if statement you can test if the variable is not None but you got an exception if you try to read an object/variables which doesn't exist.


Marco Bettio

Reply With Quote
  #5  
Old July 28th, 2003, 07:54 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Hi again Tio,

The if stament only causes an error on the interactive session. In programs it seems to work fine. but yeah, i see what you mean.

Thanks, got the vars() to work. can you use isinstance to check if a var exists on the main stage?

Ta,
Mark.

Reply With Quote
  #6  
Old July 28th, 2003, 08:32 AM
Tio Tio is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 5 Tio User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
hi netytan,

Code:
class class1(object):
        pass

class class2:
        pass

B = class1()

isinstance(B,object)
isinstance(B,class1)
isinstance(B,class2)


executing the code here above you can see that B is an instance of class object (inherited) and an instance of class class1 but not an instance of class2.

HTH
Marco Bettio

Reply With Quote
  #7  
Old July 28th, 2003, 09:06 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Thanks i get it now..

isinstance(variable, type)

where type is variables type or object which can be anything (i.e. str = string, dict = dictionary)

Thanks again, good to know.

Take care,
Mark.

Reply With Quote
  #8  
Old July 28th, 2003, 11:42 AM
TheBlackMamba TheBlackMamba is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 25 TheBlackMamba User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to TheBlackMamba Send a message via Yahoo to TheBlackMamba
Alternate solution

Ironically, a couple of hours after posting this question, I thought up a solution.
Here it is:


Exist = 0
try:
DummyVar = Var2Chk
Exist = 1
except NameError:
Exist = 0


The tabs got screwed up, but you get hte idea.

Last edited by TheBlackMamba : July 28th, 2003 at 11:47 AM.

Reply With Quote
  #9  
Old July 29th, 2003, 01:03 PM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 43 sec
Reputation Power: 6
Send a message via ICQ to SolarBear Send a message via MSN to SolarBear
Actually your last instruction is useless.
Code:
exists = False
try:
    dummy = somevar
    exists = True
except : pass

If somevar were to not exist, exists would remain False anyway, because program flow stops at the first error in try.
__________________
Time is the greatest of teachers ; sadly, it kills all of its students.
- Hector Berlioz

Reply With Quote
  #10  
Old July 29th, 2003, 01:20 PM
karsh44's Avatar
karsh44 karsh44 is offline
Just another guy
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jun 2003
Location: Wisconsin
Posts: 2,915 karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 13 h 6 m 22 sec
Reputation Power: 76
Love the sig, SolarBear

Reply With Quote
  #11  
Old July 29th, 2003, 04:59 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Quote:
Originally posted by SolarBear
Actually your last instruction is useless.
Code:
exists = False
try:
    dummy = somevar
    exists = True
except : pass

If somevar were to not exist, exists would remain False anyway, because program flow stops at the first error in try.


Hi SolarBear,

What exactly does this mean? if your talking about the except pass stament.. All try statments must have at least one except with an optional final. Since we don't want anything to happen if 'somevar' doesn't exist the script uses pass (the most logical way of doing nothing), optionally the except block could contain exists = False for a totally self contained block..

Code:
try:
	dummy = somevar
	exists = True
except:
	exists = False


This could also easily be built into a function and return True/False. But Why use this when you already have isinstance?

Have fun,
Mark.

Reply With Quote
  #12  
Old July 29th, 2003, 05:46 PM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 7 m 43 sec
Reputation Power: 6
Send a message via ICQ to SolarBear Send a message via MSN to SolarBear
Me, I didn't read your post it seems. My bad

karsh44 : MathType is your friend.

Reply With Quote
  #13  
Old July 29th, 2003, 06:13 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Not a problem Bear , Oh, whats with all the maths *looks puzzled* never was too good at that.

Mark.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Checking var existance


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