[Python] How to annotate a generator in Python 3?

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

  1. Stack

    Stack Membro Participativo

    Python 3.x supports (optional) function annotations:

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


    I sometimes encounter problems as to how to represent a given "type", and this time, I have a function that returns a generator:

    def myfunc(x: [int]) -> "generator that returns ints":
    # ^~~~~~~~~~~~~~~~~~~~~~~~~~
    return (n for n in x if n % 2 == 0)


    How should I annotate the return value? Is there any reference I can consult?

    Continue reading...

Compartilhe esta Página