问题:如何在类型提示中指定函数类型?

我想在当前的Python 3.5项目中使用类型提示。我的函数应该接收一个函数作为参数。

如何在类型提示中指定类型函数?

import typing

def my_function(name:typing.AnyStr, func: typing.Function) -> None:
    # However, typing.Function does not exist.
    # How can I specify the type function for the parameter `func`?

    # do some processing
    pass

我检查了PEP 483,但在那里找不到函数类型提示。

I want to use type hints in my current Python 3.5 project. My function should receive a function as parameter.

How can I specify the type function in my type hints?

import typing

def my_function(name:typing.AnyStr, func: typing.Function) -> None:
    # However, typing.Function does not exist.
    # How can I specify the type function for the parameter `func`?

    # do some processing
    pass

I checked PEP 483, but could not find a function type hint there.


回答 0

正如@jonrsharpe在评论中指出的,可以使用以下方法完成typing.Callable

from typing import AnyStr, Callable

def my_function(name: AnyStr, func: Callable) -> None:

问题是,Callable将其本身翻译为Callable[..., Any]

一个Callable接受任意数量的/类型的参数,并返回任何类型的值。在大多数情况下,这不是您想要的,因为您几乎可以允许传递任何函数。您也希望提示函数参数和返回类型。

这就是为什么许多typesin typing重载以支持表示这些额外类型的子脚本的原因。因此,例如,如果您有一个函数sum接受两个ints并返回一个int

def sum(a: int, b: int) -> int: return a+b

您的注释为:

Callable[[int, int], int]

也就是说,参数在外部订阅中带有下标,返回类型作为外部订阅中的第二个元素。一般来说:

Callable[[ParamType1, ParamType2, .., ParamTypeN], ReturnType]

As @jonrsharpe noted in a comment, this can be done with typing.Callable:

from typing import AnyStr, Callable

def my_function(name: AnyStr, func: Callable) -> None:

Issue is, Callable on it’s own is translated to Callable[..., Any] which means:

A callable takes any number of/type of arguments and returns a value of any type. In most cases, this isn’t what you want since you’ll allow pretty much any function to be passed. You want the function parameters and return types to be hinted too.

That’s why many types in typing have been overloaded to support sub-scripting which denotes these extra types. So if, for example, you had a function sum that takes two ints and returns an int:

def sum(a: int, b: int) -> int: return a+b

Your annotation for it would be:

Callable[[int, int], int]

that is, the parameters are sub-scripted in the outer subscription with the return type as the second element in the outer subscription. In general:

Callable[[ParamType1, ParamType2, .., ParamTypeN], ReturnType]

回答 1

另一个需要注意的有趣点是,您可以使用内置函数type()来获取内置函数的类型并使用它。所以你可以

def f(my_function: type(abs)) -> int:
    return my_function(100)

或那种形式的东西

Another interesting point to note is that you can use the built in function type() to get the type of a built in function and use that. So you could have

def f(my_function: type(abs)) -> int:
    return my_function(100)

Or something of that form


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。