[英]what exactly is python typing.Callable?
I have seen typing.Callable
, but I didn't find any useful docs about it.我见过
typing.Callable
,但我没有找到任何有用的文档。 What exactly is typing.Callable
?究竟是什么
typing.Callable
?
typing.Callable
is the type you use to indicate a callable . typing.Callable
是您用来指示可调用的类型。 Most python types that support the ()
operator are of the type collections.abc.Callable
.大多数支持
()
运算符的 python 类型collections.abc.Callable
类型。 Examples include functions, classmethod
s, staticmethod
s, bound methods and lambdas.示例包括函数、
classmethod
、 staticmethod
方法、绑定方法和 lambda。
In summary, anything with a __call__
method (which is how ()
is implemented), is a callable.总之,任何带有
__call__
方法(这就是()
的实现方式)的东西都是可调用的。
Starting with Python 3.11, you won't need to use typing.Callable
explicitly.从 Python 3.11 开始,您无需显式使用
typing.Callable
。 PEP 677 introduces implicit tuple-with-arrow syntax, so that something like Callable[[int, str], list[float]]
could be expressed much more intuitively as (int, str) -> list[float]
. PEP 677引入了隐式 tuple-with-arrow 语法,因此像
Callable[[int, str], list[float]]
这样的东西可以更直观地表示为(int, str) -> list[float]
。
See the documentation here .请参阅 此处的文档。
Short version, Callable
is a type hint that indicates a function or other object which can be called.简短版本,
Callable
是一种类型提示,指示可以调用的 function或其他 object。
Consider a simple example below.考虑下面的一个简单示例。 The
bar
parameter is a callable object that takes two ints as parameters and returns an int. bar
参数是一个可调用的 object,它接受两个 int 作为参数并返回一个 int。
>>> def foo(bar: Callable[[int, int], int], a: int, b: int) -> int:
... return bar(a, b)
...
>>> foo(int.__add__, 4, 5)
9
>>> class A:
... def __call__(self, a, b):
... return a * b
...
>>> foo(A(), 6, 7)
42
>>> foo(lambda x, y: x - y, 8, 3)
5
>>>
The typing
module is used for type hints: typing
模块用于类型提示:
This module provides runtime support for type hints.
该模块为类型提示提供运行时支持。
The documentation provides this example:该文档提供了此示例:
def greeting(name: str) -> str:
return 'Hello ' + name
In the function
greeting
, the argument name is expected to be of typestr
and the return typestr
.在 function
greeting
中,参数名称应为str
类型,返回类型为str
。 Subtypes are accepted as arguments.子类型被接受为 arguments。
typing.Callable
typing.Callable
Assume you want to define a function that takes two integers and performs some kind of operation on them that returns another integer:假设您要定义一个 function,它接受两个整数并对它们执行某种操作,返回另一个 integer:
def apply_func(a: int, b: int, func) -> int:
return func(a, b)
So the expected type of the func
argument in apply_func
is "something that can be called (eg a function) that takes two integer arguments and returns an integer":因此,
apply_func
中func
参数的预期类型是“可以调用的东西(例如函数),它需要两个 integer arguments 并返回一个整数”:
typing.Callable[[int, int], int]
Using type hints enables you to perform type checking.使用类型提示使您能够执行类型检查。 If you use an IDE like PyCharm or Visual Studio Code, you'll get visual feedback if you're using unexpected types:
如果您使用 IDE (如 PyCharm 或 Visual Studio Code),如果您使用意外类型,您将获得视觉反馈:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.