【发布时间】:2023-01-11 04:32:26
【问题描述】:
汽车.py:
class Car(object):
def __init__(self, year=2023, speed=50):
self.year = year
self.speed = speed
self.word_index = {}
工具.py:
from custom.Car import Car
c1 = Car(2020, 40)
picklefile = open('car.pkl', 'wb')
pickle.dump(c1, picklefile)
with open('car.pkl', 'rb') as f:
c2 = Car(pickle.load(f))
加载文件后,整个 Car 对象被分配给 self.year。所以我最终有: c2.year:序列化的 Car 对象。 c2.speed:默认速度为 50 而不是 40。 我错过了什么?
【问题讨论】: