0
我正在嘗試在我的Python 3代碼中實現PEP-484。而在下面的練習問題,它看起來像工作:Python類型提示 - 如何提示可選參數的默認值不是None?
def fetch_n(what: str, n="all") -> List[obj]:
query = "some sql string"
if n == "all":
# do the fetching
elif isinstance(n, int):
query = query + " LIMIT ?"
# do the fetching
else:
raise ValueError
是否有可能暗示n
在函數定義是 - const str or int
?如果是的話,該怎麼做?
我讀了cheat-sheet,目前我使用的是from typing import Optional
和n: Optional[int]
,但它不能按照需要工作。
聯盟在這裏是正確的。 +1 –