<div class="gmail_quote">On Sun, Jan 29, 2012 at 1:47 AM, Lee <span dir="ltr"><<a href="mailto:lchaplin13@gmail.com">lchaplin13@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I was afraid that a list/set/dictionary and alike is the answer, but,<br>
anyway, thanks everybody.<br>
<span class="HOEnZb"></span><br></blockquote></div><br>It doesn't seem too bad to keep track of the instances in the class object using weak references (<a href="http://docs.python.org/py3k/library/weakref.html">http://docs.python.org/py3k/library/weakref.html</a>). Here's an example that seems to do what you're asking using python 3.2, but it should be pretty similar in python 2:<br>
<br>import weakref<br><br>class A:<br> _instances = set()<br> def __init__(self):<br> self.myname = 'IamA'<br> print('This is A')<br> self.__class__._instances.add(weakref.ref(self))<br>
def foo(self):<br> print("foo")<br> def update(self):<br> for ref in self.__class__._instances:<br> obj = ref()<br> if obj is not None:<br> print("The only friends I've got are ", ref, obj.myname)<br>
<br><br>If you're creating lots of instances of A and deleting them, it would probably be worth removing the old weakrefs from the _instances set instead of just ignoring them when calling update().<br><br>-- <br>Jerry<br>