我有一個函數將函數作爲參數之一,但取決於函數可能是幾個函數之一的上下文(它們都是用於創建sorted
方法的規則的比較函數)。有沒有什麼辦法可以檢查哪個函數被傳入函數?我在想什麼是這樣的條件邏輯:檢查函數傳遞到函數中的函數
def mainFunction (x, y, helperFunction):
if helperFunction == compareValues1():
do stuff
elif helperFunction == compareValues2():
do other stuff
等會這樣嗎?我是否需要在檢查函數的所有參數時通過?有沒有更好的辦法?
如果什麼用戶創建自己的功能? –