【发布时间】:2021-10-15 23:02:19
【问题描述】:
我正在尝试在 Python 中使用类型注释。大多数情况都很清楚,除了那些将另一个函数作为参数的函数。
考虑以下示例:
from __future__ import annotations
def func_a(p:int) -> int:
return p*5
def func_b(func) -> int: # How annotate this?
return func(3)
if __name__ == "__main__":
print(func_b(func_a))
输出只是打印15。
func_b( )中的func参数应该如何注释?
【问题讨论】:
-
from typing import Callable -
这是注释“函数对象”的标准方式吗?
-
来自文档:Callable 可用于“期望特定签名的回调函数的框架”
标签: python python-3.x annotations