简体   繁体   English

python typing.Callable 到底是什么?

[英]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.示例包括函数、 classmethodstaticmethod方法、绑定方法和 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.该模块为类型提示提供运行时支持。

What are 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 type str and the return type str .在 function greeting中,参数名称应为str类型,返回类型为str Subtypes are accepted as arguments.子类型被接受为 arguments。

How to use 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_funcfunc参数的预期类型是“可以调用的东西(例如函数),它需要两个 integer arguments 并返回一个整数”:

typing.Callable[[int, int], int]

Why bother with type hints in the first place?为什么首先要打扰类型提示?

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),如果您使用意外类型,您将获得视觉反馈:

PyCharm 中的意外类型标注

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python @singledispatch 注册 Typing.Callable? - How to register typing.Callable with Python @singledispatch? python 带有 kwargs 的 function 的打字签名(typing.Callable) - python typing signature (typing.Callable) for function with kwargs Python inspect.Signature from typing.Callable - Python inspect.Signature from typing.Callable 如何在 Python 中使用辅助函数作为 typing.Callable 参数和 playwright page.wait_for_event 函数? - How do I use a helper function as typing.Callable argument with the playwright page.wait_for_event function in Python? 使用类型提示注释使用 types.FunctionType 还是 typing.Callable? - Using type hint annotation using types.FunctionType vs typing.Callable? 我可以使用 typing.Callable 类型来自动注释 function 吗? 如何? - Can I use a typing.Callable type to annotate a function automatically? How? 可调用类的 Python 类型 - Python Typing for a Callable Class 在python中将Callable键入特定的方法 - Typing Callable in python to specific methods Python 输入 Callable class 属性 - Python typing Callable class property 在 python 中键入 Class 方法 __init__ 我对 1 属性的参数类型注释是:Callable = None,注释 state 是什么? - In python typing in the Class method __init__ the argument type annotation I have for 1 attribute is: Callable = None, what does the annotation state?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM