使用从字典中选择的关键字参数调用函数

时间:2016-07-09 22:19:20

标签: python dictionary kwargs

我想从字典中调用带有关键字参数的函数。我想出了如何使用** kwargs一次为一个函数执行此操作。但我有一个包含多个函数的关键字参数的字典。这可能吗?

#!/usr/bin/env python2

yummy_stuff = {"strawberries" : "these red small thingies",
               "apples": "this round hand-sized fruit",
               "oranges": "the yellow ball thing with the skin",
               "waffles": "these flat buttery discs",
               "icecream": "the cold cone stuff",
               "chocolate": "the brown melty stuff that comes in bars"}

def return_fruit(strawberries, apples, oranges):
    return "You know, {0}, {1} and {2} are yummy!".format(strawberries, apples, oranges)

def return_dessert(waffles, icecream, chocolate):
    return "But {0}, {1} and {2} are nice as well!".format(waffles, icecream, chocolate)

print return_fruit(**yummy_stuff)

-> TypeError: return_fruit() got an unexpected keyword argument 'chocolate'

非常感谢!

0 个答案:

没有答案