Python 蟒蛇泡菜坏了?
当我尝试pickle和unpickle自定义类的对象时,我收到了一个奇怪的错误Python 蟒蛇泡菜坏了?,python,python-3.x,typeerror,pickle,deque,Python,Python 3.x,Typeerror,Pickle,Deque,当我尝试pickle和unpickle自定义类的对象时,我收到了一个奇怪的错误 from collections import deque class Buffer(deque): def __init__(self, maxlen=cf.BUFFER_SIZE): super().__init__(maxlen=maxlen) 类包含一些附加功能,但即使如此简单的实现也会产生错误 复制错误的代码: pickle.loads(pickle.dumps(Buffer())
from collections import deque
class Buffer(deque):
def __init__(self, maxlen=cf.BUFFER_SIZE):
super().__init__(maxlen=maxlen)
类包含一些附加功能,但即使如此简单的实现也会产生错误
复制错误的代码:
pickle.loads(pickle.dumps(Buffer()))
错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-60-70ac6b3f6f8f> in <module>
----> 1 pickle.loads(pickle.dumps(Buffer(), fix_imports=False))
TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在里面
---->1 pickle.load(pickle.dumps(Buffer(),fix_imports=False))
TypeError:\uuuu init\uuuuu()接受1到2个位置参数,但给出了3个
Python版本:3.7.5(默认值,2019年10月25日,15:51:11)[GCC 7.3.0]当
pickle
尝试重新构造Buffer
对象时,它将被传递基础deque
所采用的所有参数,这些参数既是iterable,也是maxlen值。您还需要接受iterable
参数,并在super()调用中传递它。想想看——如果无法为缓冲区指定任何内容,你怎么可能恢复一个pickle缓冲区呢?