我要定義一個方法的屬性:OOP屬性定義
class variables:
def definition(self):
self.word = "apple"
然後我想使用已定義的屬性:
test = variables()
print test.definition.word
而不是寫'apple'
我得到一個錯誤:
Traceback (most recent call last):
File "bezejmenný.py", line 6, in <module>
print test.definition.word
AttributeError: 'function' object has no attribute 'word'
應是**兩條獨立的行** - 調用方法('test.definition()')*然後*訪問它創建的屬性('print test.word')。你應該真的遵循結構化的Python OOP教程,這是非常基本的東西。你也不符合[風格指南](http://www.python.org/dev/peps/pep-0008/),在'__init__'之外創建一個新的屬性是一種糟糕的形式。 – jonrsharpe