weakref list in python

后端 未结 6 1331
感情败类
感情败类 2021-02-04 05:08

I\'m in need of a list of weak references that deletes items when they die. Currently the only way I have of doing this is to keep flushing the list (removing dead references ma

6条回答
  •  忘了有多久
    2021-02-04 05:30

    Use a callback function passed to second argument of a weakref.

    This code should function:

    import weakref
    
    class weakRefList(list):
    
        def addReference(self, obj):
            self._references.append(weakref.proxy(obj, self.remove))
    

提交回复
热议问题