【问题标题】:Python type annotations in parameters for functions [duplicate]函数参数中的Python类型注释[重复]
【发布时间】:2020-01-28 19:10:34
【问题描述】:

我知道 python 可以在函数中注释类型,所以类似于

def sum(x: int, y: int) -> int:
    return x + y

有可能。

如果我想传入一个函数作为参数怎么办?当我尝试这样做时,Python 给了我一个错误。

例如,

def another_func():
    #some func

def sum(another_func: **Function**, x: int, y: int) -> int:
    #some func

粗体部分确实有效。如何注释函数类型?

【问题讨论】:

    标签: python


    【解决方案1】:
    from typing import Callable
    def sum(another_func: Callable, x: int, y: int) -> int: [...]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 2021-05-28
      • 1970-01-01
      • 1970-01-01
      • 2018-08-01
      • 1970-01-01
      • 2020-04-01
      相关资源
      最近更新 更多