s1。py中的代码: class Foo : def __init__ ( self , name ): self . name = name import pickle obj = Foo ( "Git" ) pickle . dump ( obj , open ( "qb" , "wb" )) s2中的代码为: import pickle from s1 import Foo obj = pickle . load ( op
s1。py中的代码:
class Foo:def __init__(self,name):
self.name=name
import pickle
obj=Foo("Git")
pickle.dump(obj,open("qb","wb"))
s2中的代码为:
import picklefrom s1 import Foo
obj=pickle.load(open("qb","rb"))
print(obj)#一定要导入相关的类只有在s2中导入了s1中的类,代码才可以不报错