#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    25
    Rep 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.
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    5
    Rep 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.
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    5
    Rep 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
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    5
    Rep 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
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    25
    Rep 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.
  16. #9
  17. onCsdfeu
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Canada
    Posts
    100
    Rep Power
    11
    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
  18. #10
  19. Just another guy
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Jun 2003
    Location
    Wisconsin
    Posts
    2,953
    Rep Power
    261
    Love the sig, SolarBear
  20. #11
  21. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
  22. #12
  23. onCsdfeu
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Canada
    Posts
    100
    Rep Power
    11
    Me, I didn't read your post it seems. My bad

    karsh44 : MathType is your friend.
    Time is the greatest of teachers ; sadly, it kills all of its students.
    - Hector Berlioz
  24. #13
  25. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Not a problem Bear , Oh, whats with all the maths *looks puzzled* never was too good at that.

    Mark.

IMN logo majestic logo threadwatch logo seochat tools logo