[英]Higher-order function data types in python
我必須輸入提示這個 function 我知道它需要一個列表和一個 function f。 這很清楚。 它采用列表中的 int 並執行“與 function f 相關的操作,並將其附加到字符串中。但是 f: 作為類型提示輸入了什么?可調用,然后呢?
def g(L: list[int], f: Callable[[], ]) -> str:
result = ""
for el in L:
result+=f(el)
return result
Mypy output:
data_types_2.py:13: error: Unsupported operand types for + ("str" and "int")
result+=f(el)
第一個列表應該是 arguments 到f
的類型; 最后一個元素應該是它的返回類型。
我找到了解決方案
def g(L: list[int], f: Callable[[int], str]) -> str:
result = ""
for el in L:
result+=f(el)
return result
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.