[Python] What do square brackets mean in Python function definitions? [duplicate]

Discussão em 'Python' iniciado por Stack, Junho 21, 2025.

  1. Stack

    Stack Membro Participativo

    While watching a video about Python features, I came across the following code snippet:

    def compose[_First, _Second, _Thirt](
    first: Callable[[_First], _Second],
    second: Callable[[_Second], _Third],
    ) -> Callable[[_First], _Third]:
    return lambda x: second(first(x))


    I’m curious about the syntax with square brackets right after the function name, specifically [_First, _Second, _Third]. What is this syntax called? When and how is it used in Python?


    P.S. Summary for future readers:

    1. This is called Type Parameter Syntax
    2. It was introduced in Python 3.12 and is detailed in PEP 695
    3. A partial answer to the question is provided in this article on SO
    4. This specific case is described in the documentation on generic functions

    Continue reading...

Compartilhe esta Página