2015-04-08 68 views
-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?

+0

爲什麼'me.hello()'? – thinkerou

+0

@thinkerou我只想在'me'實例上調用'hello()'方法 – anilmwr

+0

但是,'me'和class MyClass'無關緊要。 – thinkerou

回答

1

您正在使用實例方法作爲類方法。使其成爲實例方法。

# remove @classmethod decorator 
# called on instance: me.hello() 

def hello(self): 
    print('Hello ' + self.name) 
相關問題