En Fri, 16 May 2008 20:44:00 -0300, Terry <[EMAIL PROTECTED]> escribió:

Is there a simple way to get all the instances of one class? I mean
without any additional change to the class.

Try with gc.get_referrers()

py> import gc
py> class A(object): pass
...
py> a,b,c = A(),A(),A()
py> A
<class __main__.A at 0x00A3F4E0>
py> for item in gc.get_referrers(A): print type(item)
...
<type 'getset_descriptor'>
<type 'getset_descriptor'>
<type 'tuple'>
<class '__main__.A'>
<class '__main__.A'>
<class '__main__.A'>
<type 'dict'>
<type 'dict'>

We need to filter that list, keeping only A's instances:

py> [item for item in gc.get_referrers(A) if isinstance(item,A)]
[<__main__.A object at 0x00A40DC8>, <__main__.A object at 0x00A40DF0>, <__main__.A object at 0x00A40E18>]

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to