使用函数在Python中创建函数

时间:2010-01-20 00:12:54

标签: python function

我是编程新手,如果可以使用函数根据输入的信息创建另一个函数,我很感兴趣:

def get_new_toy(self):
    new_toy = gui.multenterbox(
        msg = 'Enter the data for the new toy:',
        title = 'New Toy',
        fields = ('Toy Name', 'Fun for 0 to 5', 'Fun for 5 to 7', 'Fun for 7 to 10', 'Fun for over 10'))
    method = getattr(PotatoHead, new_toy[0])
    def method(self):

我不认为我这样做是正确的

感谢任何帮助

--- ---编辑

很抱歉没说清楚:

我正在为我的妹妹创造一种虚拟的宠物游戏。

'PotatoHead'可以“扮演”某些玩具,具体如下:

   def play_Softball(self):
        self.happiness_num = float(self.happiness)
        if float(self.age) <= 3.0:
            self.happiness_num = self.happiness_num + 0.3
        elif float(self.age) > 3.0 and float(self.age) < 4.0:
            self.happiness_num = self.happiness_num + 0.7
        elif float(self.age) > 4.0 and float(self.age) < 7.0:
            self.happiness_num = self.happiness_num + 1.0
        elif float(self.age) > 7.0 and float(self.age) < 9.0:
            self.happiness_num = self.happiness_num + 0.5
        elif float(self.age) > 9.0:
            self.happiness_num = self.happiness_num + 0.02
            gui.msgbox(
                msg = 'This toy is Only providing minimal fun to your potato head. Either get a new one or play with another!',
                title = 'WARNING!',
                ok_button = 'Understood')
        self.happiness = str(self.happiness_num)

我希望有一个功能允许创建一个新的'玩具'作为一个与上面类似的功能。

再次感谢您的帮助

--- --- UPDATE

再次感谢,但只是想知道

是否有可能将这样的东西加入其中:

'play_' + new_toy[0] = myPotatoHead.create_toy(new_toy[1], new_toy[2], new_toy[3], new_toy[4], new_toy[5])

在此背景下:

    def get_new_toy(self):
        new_toy = gui.multenterbox(
            msg = 'Enter the data for the new toy:',
            title = 'New Toy',
            fields = ('Toy Name', 'Fun for 0 to 3', 'Fun for 3 to 4', 'Fun for 4 to 7', 'Fun for 7 to 9', 'Fun for over 9'))
        'play_' + new_toy[0] = myPotatoHead.create_toy(new_toy[1], new_toy[2], new_toy[3], new_toy[4], new_toy[5])
        self.toys.append(new_toy[0])

再次感谢您的帮助

1 个答案:

答案 0 :(得分:2)

这是一个示例python函数,它根据传入的参数返回一个新函数。我不确定你要做什么,但这可能有助于指明你正确的方向。

def add_to(amount):
    def f(x):
        return x + amount
    return f

if __name__ == "__main__":
    add_2 = add_to(2)
    add_3 = add_to(3)

    print add_2(42)
    print add_3(42)

鉴于你的玩具示例(这对你妹妹来说似乎是一件有趣的事情),你可能会尝试这样的事情(我没有测试过代码,但它应该有所帮助):

def create_toy(self, fun_0_3, fun_3_4, fun_4_7, fun_7_9, fun_9_plus):
    def toy(self):
        self.happiness_num = float(self.happiness)
        if float(self.age) <= 3.0:
            self.happiness_num = self.happiness_num + fun_0_3
        elif float(self.age) > 3.0 and float(self.age) < 4.0:
            self.happiness_num = self.happiness_num + fun_3_4
        elif float(self.age) > 4.0 and float(self.age) < 7.0:
            self.happiness_num = self.happiness_num + fun_4_7
        elif float(self.age) > 7.0 and float(self.age) < 9.0:
            self.happiness_num = self.happiness_num + fun_7_9
        elif float(self.age) > 9.0:
            self.happiness_num = self.happiness_num + fun_9_plus
            gui.msgbox(
                msg = 'This toy is Only providing minimal fun to your potato head. Either get a new one or play with another!',
                title = 'WARNING!',
                ok_button = 'Understood')
        self.happiness = str(self.happiness_num)
    return toy

#somewhere else
play_Softball = create_toy(0.3, 0.7, 1.0, 0.5, 0.02)