代码之家  ›  专栏  ›  技术社区  ›  Eric O. Lebigot

Python中的自定义实例取消勾选:是应该更新对象字典,还是替换它?

  •  1
  • Eric O. Lebigot  · 技术社区  · 16 年前

    __setstate__ ,我想这样做是安全的

    def __setstate__(self, dict_returned_by_pickle):
        self.__dict__.update(dict_returned_by_pickle)
    

    answer here on stackoverflow .

    不过,这是否一项安全措施,以防万一 self.__dict__ 包含一些数据时 __setstate__

    self.__dict__ = dict_returned_by_pickle
    

    这迫使老年人 自述__ 垃圾收集?

    1 回复  |  直到 9 年前
        1
  •  0
  •   John La Rooy    16 年前

    更换它是好的。这就是问题所在 borg pattern

    如果没有其他引用,原始dict将被垃圾收集。