這是一個例子:
def clean_datetime(x):
return x
def func(clean_datetime = True):
if clean_datetime:
return clean_datetime(1)
func(True)
這將回傳一個錯誤TypeError: 'bool' object is not callable。有沒有辦法我不需要更改函式引數名稱?
uj5u.com熱心網友回復:
從技術上講,您可以將函式拉出globals(),但與僅重命名函式或引數相比,這是一件非常糟糕的事情。
In [53]: def clean_datetime(x):
...: return x
...:
...: def func(clean_datetime = True):
...: if clean_datetime:
...: return globals()['clean_datetime'](1)
...:
...: func(True)
Out[53]: 1
uj5u.com熱心網友回復:
就像評論中已經建議的那樣,更改函式名稱或引數名稱是 IMO 更好的選擇。它使代碼不那么模棱兩可。
一種解決方法是為函式創建別名或創建呼叫必要函式的函式。
def clean_datetime(x):
return x
alias = clean_datetime
def func(clean_datetime = True):
if clean_datetime:
return alias(1) # Calls clean_datetime(1)
func(True) # Returns 1
uj5u.com熱心網友回復:
只有一種方法:您需要更改函式名稱或引數名稱
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/415253.html
標籤:
上一篇:為appium派生Xpath
