我有一些腌制格式的数据,其中包含以下对象的实例:
Point = namedtuple('Point', ['x',])
Run Code Online (Sandbox Code Playgroud)
现在我想扩展 Point 对象以添加另一个变量“y”,但也要保持它与我已经腌制的数据兼容。
以下是我尝试过的,但似乎失败了。我还尝试通过在创建 Point 对象时将 y 参数设置为可选来使用代码来创建 Point 类,但这效果不佳。关于如何进行的任何想法?
from collections import namedtuple
Point = namedtuple('Point', ['x'])
i = Point(1)
import cPickle
pick=cPickle.dumps(i)
Point = namedtuple('Point', ['x', 'y'])
cPickle.loads(pick)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 22, in __repr__
TypeError: not enough arguments for format string
Run Code Online (Sandbox Code Playgroud)