[英]How can I define python function for this input
我需要定义一个名为 get function 的 function 以获得低于输入
ls = get_func(['square', 'circle', 'rectangle', 'triangle'])
//if it was square get one input ,circle would get two input ,rectangle get get three input
print(ls[0](1))
print(ls[1](2))
print(ls[2](2, 4))
print(ls[3](4, 5))
以下是定义返回函数列表的 function 的方法:
def get_func(func_names):
"""Return a list of functions corresponding to a list of names."""
def square(x):
return x * x
def circle(x, y):
return x * y
def rectangle(x, y):
pass
def triangle(x, y):
pass
func_by_name = {
'square': square,
'circle': circle,
'rectangle': rectangle,
'triangle': triangle,
}
return [func_by_name[name] for name in func_names]
返回具有不同签名的函数的 function 是获得有趣错误的好方法,但由于没有关于您要做什么的上下文,我无法建议更好的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.