Python Pickle 中的已知错误?
Known Bug in 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['x'] = 'y'
print(pickle.load(open('test.p', 'rb')).d)
我希望输出
y
y
然而,实际输出是
y
{}
这是已知错误还是我误解了什么?
我在 Windows.
上使用 Miniconda Python 3.5.2
... when class instances are pickled, their class’s code and data are not pickled along with them. Only the instance data are pickled.
您看到的行为已记录在案,没有错误。
给定以下代码:
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... when class instances are pickled, their class’s code and data are not pickled along with them. Only the instance data are pickled.
您看到的行为已记录在案,没有错误。