使用函数在Python中创建函数

使用函数在Python中创建函数,python,function,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

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

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)
在这方面:

    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])

再次感谢您的帮助

这里是一个示例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、fun_7、fun_9、fun_plus):
def玩具(自我):
self.happiness\u num=浮动(self.happiness)
如果浮动(自身年龄)3.0且浮动(自身年龄)<4.0:
self.happiness\u num=self.happiness\u num+乐趣
elif浮动(自身年龄)>4.0,浮动(自身年龄)<7.0:
self.happiness\u num=self.happiness\u num+乐趣
elif浮动(自身年龄)>7.0,浮动(自身年龄)<9.0:
self.happiness\u num=self.happiness\u num+乐趣
elif浮动(自身年龄)>9.0:
self.happiness\u num=self.happiness\u num+乐趣
gui.msgbox(
msg='这个玩具只给你的土豆头带来了最小的乐趣。要么买一个新的,要么玩另一个!',
标题='警告!',
确定按钮=‘已理解’)
self.happiness=str(self.happiness\u num)
归还玩具
#其他地方
玩垒球=创造玩具(0.3,0.7,1.0,0.5,0.02)

不清楚您的目标是什么。您希望新创建的函数做什么?您完全可以从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)