using python WeakSet to enable a callback functionality

前端 未结 2 1515
遇见更好的自我
遇见更好的自我 2020-12-06 11:09

I\'m investigating if I can implement an easy callback functionality in python. I thought I might be able to use weakref.WeakSet for this, but there is clearly something I\'

2条回答
  •  被撕碎了的回忆
    2020-12-06 11:19

    Try the following changes:

    To update the WeakSet:

    a1.destroyCallback.add(b)

    so the WeakSet holds a reference to b.

    Then in the __del__ method of ClassA, trigger the callback like this:

    for f in self.destroyCallback:
            f.destroyedObjectListener(self)
    

提交回复
热议问题