Weak References in python

后端 未结 4 2141
醉梦人生
醉梦人生 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:59

    The point is that they allow references to be retained to objects without preventing them from being garbage collected.

    The two main reasons why you would want this are where you do your own periodic resource management, e.g. closing files, but because the time between such passes may be long, the garbage collector may do it for you; or where you create an object, and it may be relatively expensive to track down where it is in the programme, but you still want to deal with instances that actually exist.

    The second case is probably the more common - it is appropriate when you are holding e.g. a list of objects to notify, and you don't want the notification system to prevent garbage collection.

提交回复
热议问题