1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import weakref
from sys import version
if version.startswith('3'):
raw_input = input
class WeakList(list):
def append(self, val):
list.append(self, weakref.proxy(val, self.on_delete))
def on_delete(self, val):
print('>>> pass on_delete')
_tmplist = [_item for _item in self.__iter__() if dir(_item) != []]
del self[:]
for _item in _tmplist:
list.append(self, _item)
class Toto(object):
instances = WeakList()
def __init__(self):
self.instances.append(self)
t1 = Toto()
t2 = Toto()
print('proxy >>>', Toto.instances)
del t2
print('proxy >>>', Toto.instances)
t3 = Toto()
t4 = Toto()
del t3
print('proxy >>>', t4.instances)
del t1
print('proxy >>>', t4.instances)
raw_input('fin\n') |