|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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.
|
|
#2
|
|||
|
|||
|
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. |
|
#3
|
||||
|
||||
|
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. |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
||||
|
||||
|
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. |
|
#6
|
|||
|
|||
|
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 |
|
#7
|
||||
|
||||
|
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. |
|
#8
|
|||
|
|||
|
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. |
|
#9
|
||||
|
||||
|
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 |
|
#10
|
||||
|
||||
|
Love the sig, SolarBear
|
|
#11
|
||||
|
||||
|
Quote:
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. |
|
#12
|
||||
|
||||
|
Me, I didn't read your post it seems. My bad
![]() karsh44 : MathType is your friend. |
|
#13
|
||||
|
||||
|
Not a problem Bear
, Oh, whats with all the maths *looks puzzled* never was too good at that.Mark. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Checking var existance |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|