Weak References in python

后端 未结 4 2168
醉梦人生
醉梦人生 2021-01-30 10:37

I have been trying to understand how python weak reference lists/dictionaries work. I\'ve read the documentation for it, however I cannot figure out how they work, and what the

4条回答
  •  青春惊慌失措
    2021-01-30 10:57

    Just want to point out that weakref.ref does not work for built-in list because there is no __weakref__ in the __slots__ of list. For example, the following code defines a list container that supports weakref.

    import weakref
    
    class weaklist(list):
        __slots__ = ('__weakref__',)
    
    l = weaklist()
    r = weakref.ref(l)
    

提交回复
热议问题