小编Vít*_*rka的帖子

OOP属性定义

我想在方法中定义一个属性:

\n\n
class variables:\n    def definition(self):\n        self.word = "apple"\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后我想使用定义的属性:

\n\n
test = variables()\nprint test.definition.word\n
Run Code Online (Sandbox Code Playgroud)\n\n

我没有写,而是\'apple\'收到错误:

\n\n
Traceback (most recent call last):\n  File "bezejmenn\xc3\xbd.py", line 6, in <module>\n    print test.definition.word\nAttributeError: \'function\' object has no attribute \'word\'\n
Run Code Online (Sandbox Code Playgroud)\n

python oop python-2.7

2
推荐指数
1
解决办法
353
查看次数

标签 统计

oop ×1

python ×1

python-2.7 ×1