在Python 3的unpickling中,是否有足够短的方法来调用类的__init__
构造函数?通常的方法是使用__getinitargs__
,如此
from __future__ import print_function
import pickle
class Car:
def __init__(self, model, number):
self.model = model
self.number = number
print("constructed with", model, number)
# many other things to do
def __getstate__(self):
# intentionally returns None
pass
def __setstate__(self, state):
pass
def __getinitargs__(self):
# save some information when pickling
# (will be passed to the constructor upon unpickling)
return self.model, self.number
c = Car("toyota", 1234)
d = pickle.loads(pickle.dumps(c))
print("reconstructed with", d.model, d.number)
但是,新样式类和Python 3+中将忽略__getinitargs__
,所有类只能是新样式类。有__getnewargs__
但它只会将参数传递给不同的__new__
类方法。上述说明性示例的python 2调用将导致
>> constructed with toyota 1234
>> constructed with toyota 1234
>> reconstructed with toyota 1234
虽然python 3调用会出错
>> constructed with toyota 1234
Traceback (most recent call last):
File "test.py", line 26, in <module>
print("reconstructed with", d.model, d.number)
AttributeError: 'Car' object has no attribute 'model'
并忽略__getinitargs__
方法。
我认为Python 3在这方面不会轻易倒退,所以希望我错过了一些明显的东西。
编辑:用__getinitargs__
替换__getnewargs__
并不能解决问题。
答案 0 :(得分:3)
如果您希望pickle
通过调用Car(self.model, self.number)
来取消对象的取消,通过__init__
进行初始化,就像正常调用Car
一样,然后告诉它在__reduce__
method中执行此操作:
def __reduce__(self):
return (Car, (self.model, self.number))