【发布时间】:2017-08-24 11:07:53
【问题描述】:
以下陈述:
import pickle
from collections import OrderedDict as Odict
class A(Odict):
def __init__(self, items):
super().__init__(items)
items = Odict((('a',1), ('b', 2)))
a = A(items)
with open('test.pickle','wb') as fout:
pickle.dump(a, fout)
with open('test.pickle','rb') as fin:
pickle.load(fin)
导致这个错误:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
TypeError: __init__() missing 1 required positional argument: 'items'
但是使用普通的 dict 而不是 OrderedDict 可以正常工作。我知道在这种情况下我不需要__init__,但这个问题阻止了使用带有更复杂的OrderedDict 子类的多处理模块,其中其他参数存储为属性,我无法避免使用它。 (我用的是python 3.4.6)。
【问题讨论】:
-
我想我可能已经找到了一个我认为可能对其他人有用的解决方案。添加:
标签: python python-3.x pickle ordereddictionary