The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Checking var existance
Discuss Checking var existance in the Python Programming forum on Dev Shed. Checking var existance Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 28th, 2003, 02:02 AM
|
|
Registered User
|
|
Join Date: Apr 2003
Posts: 25
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
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.
|

July 28th, 2003, 04:33 AM
|
|
Junior Member
|
|
Join Date: Jul 2003
Posts: 5
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.
|

July 28th, 2003, 07:00 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

July 28th, 2003, 07:35 AM
|
|
Junior Member
|
|
Join Date: Jul 2003
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Hi netytan,
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
|

July 28th, 2003, 07:54 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
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.
|

July 28th, 2003, 08:32 AM
|
|
Junior Member
|
|
Join Date: Jul 2003
Posts: 5
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
|

July 28th, 2003, 09:06 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
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.
|

July 28th, 2003, 11:42 AM
|
|
Registered User
|
|
Join Date: Apr 2003
Posts: 25
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
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.
|

July 29th, 2003, 01:03 PM
|
 |
onCsdfeu
|
|
Join Date: Jul 2003
Location: Canada
Posts: 100
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
|
|
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
|

July 29th, 2003, 01:20 PM
|
 |
Just another guy
|
|
Join Date: Jun 2003
Location: Wisconsin
|
|
|
Love the sig, SolarBear
|

July 29th, 2003, 04:59 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

July 29th, 2003, 05:46 PM
|
 |
onCsdfeu
|
|
Join Date: Jul 2003
Location: Canada
Posts: 100
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
|
|
Me, I didn't read your post it seems. My bad
karsh44 : MathType is your friend.
|

July 29th, 2003, 06:13 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Not a problem Bear  , Oh, whats with all the maths *looks puzzled* never was too good at that.
Mark.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|