在我使用 Python 和 pyglet 的游戏中,我有一些组将事件向下传播到其成员:
class Group(EventDispatcher):
def __init__(self):
self.members = []
def add(self, member):
self.members.append(member)
self.push_handlers(member)
def remove(self, member):
self.members.remove(member)
# and then what???
Group.register_event('on_event')
如果我del
所有对成员的引用并 remove() 它,Group 中的处理程序会阻止对象被垃圾收集吗?处理程序会消失(weakref)吗?如果没有,我该如何清理处理程序?
编辑:我跑了我的测试会话,看看会发生什么:
>>> from pyglet.event import EventDispatcher
>>> class Group(EventDispatcher):
... pass
...
>>> Group.register_event_type('on_tick')
'on_tick'
>>> g = Group()
>>> class Members:
... def on_tick(self):
... print('tick')
...
>>> m = Members()
>>> g.push_handlers(m)
>>> g.dispatch_event('on_tick')
tick
>>> del m
>>> g.dispatch_event('on_tick')
tick
>>> class B:
... def on_tick(self):
... print(self.x)
...
>>> m = B()
>>> g.push_handlers(m)
>>> g.dispatch_event('on_tick')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.2/site-packages/pyglet/event.py", line 355, in dispatch_event
if handler(*args):
File "<stdin>", line 3, in on_tick
AttributeError: 'B' object has no attribute 'x'
>>> m.x = 3
>>> g.dispatch_event('on_tick')
3
tick
>>> del m
>>> g.dispatch_event('on_tick')
3
tick
所以我猜 EventDispatcher 仍然保留对处理程序的引用。因此,问题变成了如何清理处理程序。