1 回答

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超1個(gè)贊
operator是一個(gè)模塊,而不是一個(gè)類(lèi)或類(lèi)型。您想要做的是創(chuàng)建一個(gè)可以滿足傳遞給的函數(shù)的類(lèi)型提示func。如果我們從頭開(kāi)始編寫(xiě)operator.lt,它可能看起來(lái)像這樣:
from numbers import Number
def le(a: Number, b: Number) -> bool:
...
使用Callable類(lèi)型我們可以將其寫(xiě)為:
Callable[[Number, Number], bool]
總而言之,你會(huì)得到這樣的結(jié)果:
import operator
from typing import Dict, Tuple, Callable, Optional
from numbers import Number
Operator = Callable[[Number, Number], bool]
OperatorDict = Dict[str, Tuple[Operator, float]]
def func(thresholds: Optional[OperatorDict] = None) -> None:
if thresholds is None:
thresholds = {"height": (operator.lt, 0.7), "width": (operator.gt, 0.1)}
pass
添加回答
舉報(bào)