Why doesn't the weakref work on this bound method?

后端 未结 3 1395
南笙
南笙 2020-12-06 06:00

I have a project where i\'m trying to use weakrefs with callbacks, and I don\'t understand what I\'m doing wrong. I have created simplified test that shows the exact behavio

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-06 06:43

    You want a WeakMethod.

    An explanation why your solution doesn't work can be found in the discussion of the recipe:

    Normal weakref.refs to bound methods don't quite work the way one expects, because bound methods are first-class objects; weakrefs to bound methods are dead-on-arrival unless some other strong reference to the same bound method exists.

提交回复
热议问题