簡體   English   中英

如何為此輸入定義 python function

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM