python - 如何使用自引用和带有插槽的类来 pickle 和取消 pickle 对象?

标签 python pickle slots

当对象通过其属性之一引用自身时,从带有插槽的类中挑选对象的正确方法是什么?这是一个简单的示例,使用我当前的实现,我不确定它是否 100% 正确:

import weakref
import pickle

class my_class(object):

    __slots__ = ('an_int', 'ref_to_self', '__weakref__')

    def __init__(self):
        self.an_int = 42
        self.ref_to_self = weakref.WeakKeyDictionary({self: 1})

    # How to best write __getstate__ and __setstate__?
    def __getstate__(self):

        obj_slot_values = dict((k, getattr(self, k)) for k in self.__slots__)
        # Conversion to a usual dictionary:
        obj_slot_values['ref_to_self'] = dict(obj_slot_values['ref_to_self'])
        # Unpicklable weakref object:
        del obj_slot_values['__weakref__']
        return obj_slot_values

    def __setstate__(self, data_dict):
        # print data_dict
        for (name, value) in data_dict.iteritems():
            setattr(self, name, value)
        # Conversion of the dict back to a WeakKeyDictionary:
        self.ref_to_self = weakref.WeakKeyDictionary(
            self.ref_to_self.iteritems())

这可以通过以下方式进行测试:

def test_pickling(obj):
    "Pickles obj and unpickles it.  Returns the unpickled object"

    obj_pickled = pickle.dumps(obj)
    obj_unpickled = pickle.loads(obj_pickled)

    # Self-references should be kept:
    print "OK?", obj_unpickled == obj_unpickled.ref_to_self.keys()[0]
    print "OK?", isinstance(obj_unpickled.ref_to_self,
                            weakref.WeakKeyDictionary)

    return obj_unpickled

if __name__ == '__main__':
    obj = my_class()
    obj_unpickled = test_pickling(obj)
    obj_unpickled2 = test_pickling(obj_unpickled)

这是一个正确/稳健的实现吗?如果my_class继承自一个带有__slots__的类,__getstate____setstate__应该怎么写? __setstate__ 是否因为“循环”命令而发生内存泄漏?

PEP 307中有备注这让我想知道 pickling my_class 对象是否完全有可能以一种可靠的方式:

The __getstate__ method should return a picklable value representing the object's state without referencing the object itself.

这是否与对对象本身的引用被 pickle 的事实冲突?

问题很多:任何评论、评论或建议都将不胜感激!

最佳答案

看起来原始帖子建议的效果很好。

至于 PEP 307 的内容:

The __getstate__ method should return a picklable value representing the object's state without referencing the object itself.

我理解这仅意味着 __getstate__ 方法必须简单地返回一个不指向(unpickleable)原始对象的表示。因此,返回一个引用自身的对象是可以的,只要没有对原始(unpickleable)对象的引用。

关于python - 如何使用自引用和带有插槽的类来 pickle 和取消 pickle 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2922628/

相关文章:

python - python 中的“没有名为 spacy 的模块”,但在常规 python 解释器中工作正常

Python 绘图显示异常缩放

python - 了解类型错误: '<' not supported between instances of 'Example' and 'Example'

python - 可以 pickle 然后检索 matplotlib 图形对象吗?

python - 在python中查看pickle文件中的图像

c++ - 对 SOME_SIGNAL_NAME 的 undefined reference

python - PyGame Conway 的生命游戏,重绘 Sprite

Python、__slots__、继承和类变量==>属性是只读的bug

c++ - 将一个信号连接到两个插槽,但在 Qt 中一次只执行一个插槽

python-3.x - 加载 pickle 文件时出错