Python - AttributeError: type object 'MyClass' has no attribute 'name'
我有3个python文件。
MyMeult.Py
1 2 3 4 5 6 7 | class MyClass: def __init__(self, name): self.name = name @classmethod def hello(self): print('Hello ' + self.name) |
加载程序
1 2 3 4 5 | import pickle from MyModule import * me = pickle.load(file('my_pkl.pickle','rb')) me.hello() |
傻瓜
1 2 3 4 5 | import pickle from MyModule import * me = MyClass('Anil') pickle.dump(me, open('my_pkl.pickle','wb')) |
当我执行loader.py时,出现以下错误:
1 | AttributeError: type object 'MyClass' has no attribute 'name' |
如何访问可用的名称实例?
您正在使用实例方法作为类方法。使其成为实例方法。
1 2 3 4 5 | # remove @classmethod decorator # called on instance: me.hello() def hello(self): print('Hello ' + self.name) |