我正在尝试pickle和unpickledatetime.datetime
子类对象。然而,它总是会产生错误,我不知道为什么它是和如何解决它。下面是最简单的例子:
from datetime import datetime, date, time
class A(datetime):
def __new__(cls, year = 2016, month=1, day=1, hour=0, minute=0, leap_year=False):
return datetime.__new__(cls, year ,month, day, hour, minute)
import pickle
obj = A( month=1, day=1, hour=1, leap_year = False)
serial = pickle.dumps(obj, protocol=pickle.HIGHEST_PROTOCOL)
unpickle = pickle.loads( serial, encoding='bytes')
这将产生以下错误:
TypeError Traceback (most recent call last)
<ipython-input-2-12195a09d83d> in <module>()
5
6
----> 7 unpickle = pickle.loads( serial, encoding='bytes')
<ipython-input-1-605483566b52> in __new__(cls, year, month, day, hour, minute, leap_year)
2 class A(datetime):
3 def __new__(cls, year = 2016, month=1, day=1, hour=0, minute=0, leap_year=False):
----> 4 return datetime.__new__(cls, year ,month, day, hour, minute)
5
TypeError: an integer is required (got type bytes)
有人知道问题出在哪里,怎么解决吗?你知道吗
基于
pickle
模块的documentation的Pickling Class Instances部分中的信息,我能够实现以下功能。从添加的__reduce_ex__()
方法返回的值的元组将导致在类实例被pickle.loads()
调用取消勾选时调用__new__()
构造函数,就像调用类时通常发生的情况一样。你知道吗注意,我不需要知道
datetime
是否或者如何定制pickling,也不需要了解它的C实现。你知道吗另请注意,由于您的问题中的实现忽略了
leap_year
参数(不清楚为什么需要将其传递给初始值设定项),因此我用Pythonproperty
替换了它,它根据当前实例的year值动态计算布尔值。你知道吗相关问题 更多 >
编程相关推荐