How and when to appropriately use weakref in Python

前端 未结 3 1096
借酒劲吻你
借酒劲吻你 2020-12-22 17:31

I have some code where instances of classes have parent<->child references to each other, e.g.:

class Node(object):
  def __init__(self):
    self.parent          


        
3条回答
  •  误落风尘
    2020-12-22 18:18

    Yep, weakref's excellent here. Specifically, instead of:

    self.children = {}
    

    use:

    self.children = weakref.WeakValueDictionary()
    

    Nothing else needs change in your code. This way, when a child has no other differences, it just goes away -- and so does the entry in the parent's children map that has that child as the value.

    Avoiding reference loops is up high on a par with implementing caches as a motivation for using the weakref module. Ref loops won't kill you, but they may end up clogging your memory, esp. if some of the classes whose instances are involved in them define __del__, since that interferes with the gc's module ability to dissolve those loops.

提交回复
热议问题