用 kwargs 解封新样式不可能吗?
在类的实例化过程中,我初始化了一些不可 picklable 的字段。因此,为了能够正确(取消)pickle 我的类,我希望在 unpickle 时调用我的 init 方法。这似乎是旧式类的工作方式。
对于新样式类,我需要使用 __new__
和 __getnewargs__
。这就是我所做的:
import cPickle
class Blubb(object):
def __init__(self, value):
self.value = value
class Bla(Blubb):
def __new__(cls, value):
instance = super(Bla, cls).__new__(cls)
instance.__init__(value)
return instance
def __getnewargs__(self):
return self.value,
def __getstate__(self):
return {}
def __setstate__(self, dct):
pass
x = Bla(2)
print x.value
pickled = cPickle.dumps(x, 2)
x_ = cPickle.loads(pickled)
assert x_.value == 2
如果不是因为 obj = C.__new__(C, *args)
,这样就很好了。现在有**kwargs
。因此,我的 __new__
和 __init__
方法中仅限于非关键字参数。
有谁知道解决这个问题的方法吗?这确实很不方便。
During instantiation of my class, I initialize some fields that are not picklable. Thus, in order to be able to (un)pickle my classes correctly, I would like my init method to be called on unpickling. This is, it seems, the way it worked with old-style classes.
With new style classes, I need to use __new__
and __getnewargs__
. Here is what I do:
import cPickle
class Blubb(object):
def __init__(self, value):
self.value = value
class Bla(Blubb):
def __new__(cls, value):
instance = super(Bla, cls).__new__(cls)
instance.__init__(value)
return instance
def __getnewargs__(self):
return self.value,
def __getstate__(self):
return {}
def __setstate__(self, dct):
pass
x = Bla(2)
print x.value
pickled = cPickle.dumps(x, 2)
x_ = cPickle.loads(pickled)
assert x_.value == 2
This would be fine, if not for the fact that obj = C.__new__(C, *args)
. There is now **kwargs
. So I am restricted to non keyword arguments in my __new__
and __init__
methods.
Does anyone know of a way to solve this? This is really unconvenient.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
pickle 协议 2 想要默认调用 cls.__new__(cls, *args),但有一种方法可以解决这个问题。如果您使用 __reduce__ ,您可以返回一个函数,该函数会将您的参数映射到 __new__ 。我能够修改您的示例以使
**kwargs
正常工作:The pickle protocol 2 wants to call
cls.__new__(cls, *args)
by default, but there is a way around this. If you use__reduce__
you can return a function which will map your arguments to__new__
. I was able to modify your example to get**kwargs
to work: