gpt4 book ai didi

python - Python Pickle 中的已知错误?

转载 作者:行者123 更新时间:2023-11-30 22:45:16 24 4
gpt4 key购买 nike

给出以下代码:

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 documentation

... when class instances are pickled, their class’s code and data are not pickled along with them. Only the instance data are pickled.

您看到的行为已记录在案,并且没有错误。

关于python - Python Pickle 中的已知错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41233003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com