gpt4 book ai didi

python - 如何 pickle 字典对象?

转载 作者:太空狗 更新时间:2023-10-29 22:05:55 25 4
gpt4 key购买 nike

阅读 pickle 文档后,我的印象是一个类需要实现 __reduce____getstate__ 才能正确进行 pickle。但是字典的 pickle 是如何工作的呢?他们没有任何这些属性:

> dict(a=1).__reduce__()

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/home/daniyar/work/Apr24/<ipython-input-30-bc1cbd43305b> in <module>()
----> 1 dict(a=1).__reduce__()

/usr/lib/python2.6/copy_reg.pyc in _reduce_ex(self, proto)
68 else:
69 if base is self.__class__:
---> 70 raise TypeError, "can't pickle %s objects" % base.__name__
71 state = base(self)
72 args = (self.__class__, base, state)

TypeError: can't pickle dict objects



> dict(a=1).__getstate__()

---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/home/daniyar/work/Apr24/<ipython-input-31-00932fb40067> in <module>()
----> 1 dict(a=1).__getstate__()

AttributeError: 'dict' object has no attribute '__getstate__'

另外,从 dict 派生的类是如何 pickle 的?

最佳答案

The pickle module handles a number of types "natively" .它本地处理的类型将需要实现 "pickle protocol" .字典和简单的子类是本地处理的。

关于python - 如何 pickle 字典对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10364818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com