weakref list in python

后端 未结 6 1334
感情败类
感情败类 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:10

    Why can't you just do it like this:

    import weakref
    
    class WeakList(list):
        def append(self, item):
            list.append(self, weakref.ref(item, self.remove))
    

    And then do similar for __iadd__, extend etc. Works for me.

提交回复
热议问题