我使用 pickle 来保存对象列表。使用 pickle 保存列表并再次加载相同的列表后,我将新加载的列表与原始列表进行比较。奇怪的是,这两个对象不同。为什么会这样呢?不应该是一样的吗?
我已经尝试仅使用 init 函数中定义的实例属性,而不使用类属性,但错误仍然存在。
import pickle as p
class Person(object):
def __init__(self, name=None, job=None, quote=None):
self.name = name
self.job = job
self.quote = quote
personList = [Person("Payne N. Diaz", "coach", "Without exception, there is no rule!"),
Person("Mia Serts", "bicyclist", "If the world didn't suck, we'd all fall off!"),
Person("Don B. Sanosi", "teacher", "Work real hard while you wait and good things will come to you!")]
with open('test_list.p', 'wb') as handle:
p.dump(personList, handle, protocol=p.HIGHEST_PROTOCOL)
with open('test_list.p', 'rb') …Run Code Online (Sandbox Code Playgroud)