![](/img/trans.png)
[英]Python error: AttributeError: type object 'MyClass' has no attribute 'channel'
[英]Python - AttributeError: type object 'MyClass' has no attribute 'name'
我有3個python文件。
MyModule.py
class MyClass:
def __init__(self, name):
self.name = name
@classmethod
def hello(self):
print('Hello ' + self.name)
加載器
import pickle
from MyModule import *
me = pickle.load(file('my_pkl.pickle','rb'))
me.hello()
Dumper.py
import pickle
from MyModule import *
me = MyClass('Anil')
pickle.dump(me, open('my_pkl.pickle','wb'))
當我執行Loader.py時,出現以下錯誤:
AttributeError: type object 'MyClass' has no attribute 'name'
如何訪問可用的名稱實例?
您正在使用實例方法作為類方法。 使其成為實例方法。
# remove @classmethod decorator
# called on instance: me.hello()
def hello(self):
print('Hello ' + self.name)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.