开发者

Variables as a part of calling a method in python

I'm pretty new to programming and I'm creating a python game for my little sister.

I'm having trouble, because I want the variable value to be a part of the method name Is there any way this is possible?

def play_with_toy(self):
    toy = gui.buttonbox(
        msg = 'Choose a toy for your potato head to play with:',
        title = 'Choose a Toy开发者_Go百科',
        choices = self.toys)
    method_name = 'play_' + toy + '()'
    myPotatoHead.method_name

Using Python 2.5.4 for Mac (IDLE) and easygui 0.83

Thanks for any help


method = getattr(myPotatoHead, 'play_' + toy)
method()


getattr(myPotatoHead,"play_"+toy)()


Try this:

method = getAttr(myPotatoHead, 'play_' + toy)
method()

(sorry about the semi-colons! I was programming in javascript all day).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜