[wingide-users] Enumerating instances of a class

Michael Foord fuzzyman at voidspace.org.uk
Sun Mar 21 16:58:21 MDT 2010


On 21/03/2010 22:45, Tom Stambaugh wrote:
> Anybody know how to enumerate the instances of a Python class?
>
> The Smalltalk equivalent, on any Smalltalk "Behavior" instance (any 
> class or Metaclass), is "allInstances". It is implemented as a 
> primitive; the underlying VM traverses the active object space 
> collecting objects whose class pointer matches the class in question.
>

There is nothing builtin in Python directly equivalent, but you can add 
it yourself using something like gc.get_objects():

http://docs.python.org/library/gc.html

If you want to keep track of instances a much better way of doing it is 
have your class(es) keep of weak references to all instances it creates.

All the best,

Michael

> I'm hoping that there's some kind of builtin method that does the same 
> in Python. I *really* don't want to have to extend Python myself.
>
> Thx,
> Tom


-- 
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog

READ CAREFULLY. By accepting and reading this email you agree, on behalf of your employer, to release me from all obligations and waivers arising from any and all NON-NEGOTIATED agreements, licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap, confidentiality, non-disclosure, non-compete and acceptable use policies (”BOGUS AGREEMENTS”) that I have entered into with your employer, its partners, licensors, agents and assigns, in perpetuity, without prejudice to my ongoing rights and privileges. You further represent that you have the authority to release me from any BOGUS AGREEMENTS on behalf of your employer.




More information about the wingide-users mailing list