-1
我有3個python文件。Python - AttributeError:類型對象'MyClass'沒有屬性'名稱'
MyModule.py
class MyClass:
def __init__(self, name):
self.name = name
@classmethod
def hello(self):
print('Hello ' + self.name)
Loader.py
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'))
當我exceute Loader.p Ÿ我得到以下錯誤:
AttributeError: type object 'MyClass' has no attribute 'name'
如何訪問名稱實例vaiable?
爲什麼'me.hello()'? – thinkerou
@thinkerou我只想在'me'實例上調用'hello()'方法 – anilmwr
但是,'me'和class MyClass'無關緊要。 – thinkerou