Classes & Instances
Tim Hochberg
tim.hochberg at ieee.org
Wed Jan 2 14:26:03 EST 2002
More information about the Python-list mailing list
Wed Jan 2 14:26:03 EST 2002
- Previous message (by thread): Classes & Instances
- Next message (by thread): Classes & Instances
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
"Andrew Nguyen" <pythonnet at hotmail.com> wrote in message news:40dbad98.0201021100.2731aecd at posting.google.com... > We all know that you can't use classes without instances. > > Now, for the problem. How do I get a class, so that when an instance > is made of it, the class records the instance's name to a list. > > e.g, > class a: > pass > > f = a I assume you means "f = a()" here. If so, you probably want something like: >>> class A: ... instances = [] ... def __init__(self, name): ... self.instances.append(self) ... self.name = name ... def __repr__(self): ... return '<instance %s>' % self.name # Name is only around so this prints something legible ... >>> f = A('spam') >>> g = A('eggs') >>> h = A('more spam') >>> A.instances [<instance spam>, <instance eggs>, <instance more spam>] You don't have to make instances a part of the class A, but that's what I'd do. -tim > ---- > > how do I get f into a list. And remember, my goal is _flexability_ so: > > class a: > pass > > f = a > > foolist = ['f'] > > Will NOT work for my goal, but, if let's say there is a function, > 'TakeInstanceName()': > > class a: > foolist[:-1]=TakeInstanceName()#This function takes the Instance > name # whenever the class is refered to > > f = a > > #Now after that, foolist should have ['f'], but, my question is, what > IS the #function that would take the TakeInstanceName()'s place? > -----
- Previous message (by thread): Classes & Instances
- Next message (by thread): Classes & Instances
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list