Pyglet处理程序和删除的对象

时间:2012-09-01 22:29:05

标签: python pyglet

在我使用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我对成员的所有引用并删除()它,组中的处理程序是否会阻止对象被垃圾回收?处理程序是否会消失(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仍然保留对处理程序的引用。因此,问题就变成了如何清理处理程序。

1 个答案:

答案 0 :(得分:1)

EventDispatcher.remove_handlers与push_handlers相反。所以调用remove_handlers(m)然后del m将允许m被垃圾收集。