Python Pickle中已知的Bug?
给定以下代码:Python Pickle中已知的Bug?,python,pickle,Python,Pickle,给定以下代码: import pickle class Test: d = {} def func(self): self.d['x'] = 'y' test = Test() test.func() pickle.dump(test, open('test.p', 'wb')) %reset import pickle class Test: d = {} def func(self): self.d[
import pickle
class Test:
d = {}
def func(self):
self.d['x'] = 'y'
test = Test()
test.func()
pickle.dump(test, open('test.p', 'wb'))
%reset
import pickle
class Test:
d = {}
def func(self):
self.d['x'] = 'y'
print(pickle.load(open('test.p', 'rb')).d)
我会期待产出
y
y
然而,实际输出是
y
{}
这是一个已知的错误还是我误解了什么
我正在Windows上使用Miniconda Python 3.5.2
。。。当类实例被pickle时,它们的类的代码和数据不会随之被pickle。只有实例数据被pickle
您看到的行为是有文档记录的,没有错误。我不希望得到您期望的输出
self.d
是一个类变量。Pickle序列化对象,而不是类。每当您想知道Python标准库模块中是否存在bug时,通常可以假设答案是“不,您只是做错了什么。”Pickle的.d
不是特定于您Pickle的对象,为什么您希望它与对象一起被Pickle?在这种情况下,请回答此问题并添加新程序,或提出新问题。也许还有另一个问题。