如何使 OrderedSet 可腌制?

How can I make OrderedSet pickleable?

集合的 python 文档链接到 OrderedSet 数据类型的接收者。 class 表现得像一个集合,但保持插入顺序。

Link(在页面底部): https://docs.python.org/3/library/collections.abc.html?highlight=orderedset

Link 目标: https://code.activestate.com/recipes/576694/

我现在想让这个 class 可腌制,但双向链表会导致递归错误。作为解决方案,我已将以下方法添加到此 class:

def __getstate__(self):
    """ Avoids max depth RecursionError when dumping with pickle"""
    return list(self)

def __setstate__(self, state):
    """ Tells pickle how to restore instance using state """
    self.__init__(state)

这行得通,但我觉得在 __setstate__ 中调用 __init__ 很笨拙。此外,它需要从头开始重建链表。有没有更好的方法让这个 class 可以腌制?

正如疯狂物理学家在评论()中提到的,问题中提出的解决方案是合理的。为 OrderedSet 定义以下方法 class 为 pickle 提供它需要存储的信息,然后重建 OrderedSet 的内容:

def __getstate__(self):
    """ Avoids max depth RecursionError when dumping with pickle"""
    return list(self)

def __setstate__(self, state):
    """ Tells pickle how to restore instance using state """
    self.__init__(state)

pickle在内部会将OrderedSet的内容存储为一个list,然后可以调用OrderedSet__init__()方法重构OrderedSet =] 与存储的列表。

作为奖励,copy.deepcopy() 回落到 pickle/unpickle,因此 copy.deepcopy() 现在也可以使用。