Python 2.7 执行3属性类python

Python 2.7 执行3属性类python,python-2.7,class,Python 2.7,Class,这是我的密码 class Word: def motivation(self): return 'Dont Give up' def __call__(self, x): return 'love' def __call__(self, c): return 'hate' def dude(self): return 'dude' def brother(self): return '

这是我的密码

class Word:

    def motivation(self):
       return 'Dont Give up'
    def __call__(self, x):
       return 'love'
    def __call__(self, c):
       return 'hate'
    def dude(self):
       return 'dude'
    def brother(self):
       return 'brother'

word = Word()
有了这个代码,我必须这样生成

>>> word.motivation.dude
'Dont Give Up Dude'
一定也是这样

>>> word.motivation.brother
'Dont Give Up Brother'
如果使用此脚本执行,则输出会出错

AttributeError: 'Function' object has no attribute 'dude'


python是对的:名为
motivation
的函数没有
dude
brother
等属性。它们属于单词对象。喜欢你的单词变量。你可以写word.motivation(),word.motivation,word.dude和word.brother.Soo,我必须在哪里为word-dude和word-brother写脚本?你想准确地键入
word.motivation.dude
并看到:'不要放弃dude'?您可以在python交互式shell中使用它,但这样的构造对我来说似乎毫无意义。还是可以写点别的,比如word.motivation(“都德”)?此外,您在脚本中写了“dude”,但希望将“dude”作为输出,这是否意味着必须将第一个字母转换为大写字母?是的,这只是我使用python进行培训的示例
word.motivation.dude
word.motivation.brother
必须像上面的示例一样生成。如果我创建了一个新的
,然后与
类单词
组合,没有更改,仍然是错误的。
AttributeError: 'Function' object has no attribute 'brother'