How To capture an id into an array..

Robert Brewer fumanchu at amor.org
Sun Mar 28 20:14:52 EST 2004


BALAJI RAMAN wrote:
> class V(E):
>     """ This is a variable for MPY"""
>     def __init__(self):
>         print id(self)
>         check=[]
>         check.append(id(self))
>         self.val = None
> 
> what I do is call say v1=V() so it means that v1 belongs to the 
> variable class..
> 
> Again if I call v2=V() so now v2 also belong to this class.. I want 
> to keep a track of all the instances of this class.. and store their 
> ids in an array..

'check' is actually a list, not an array, but they're similar. You have
declared 'check' inside a function, and have not provided a means of
returning or accessing it; once the function ends, 'check' disappears.
You have to declare 'check' in a namespace which will persist. Try this
in a module:

# See how we declare 'check' outside the class?
# That means it exists in the namespace of the module,
# not the namespace of class V or its __init__ function.

check=[]

class V(E):
    """ This is a variable for MPY"""
    def __init__(self):
        print id(self)
        check.append(id(self))
        self.val = None


Hope that helps,

Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org




More information about the Python-list mailing list