Sobes.tech
Junior — Middle

Do you have practical experience in creating and describing typed generators?

sobes.tech AI

Answer from AI

Python generators allow creating iterators using the yield keyword. Typed generators are generators that explicitly specify the types of elements they return using type annotations.

Example of a typed generator:

from typing import Generator

def count_up_to(max_value: int) -> Generator[int, None, None]:
    count = 1
    while count <= max_value:
        yield count
        count += 1

# Usage
for number in count_up_to(5):
    print(number)

In this example, the count_up_to function returns a generator that sequentially yields integers from 1 to max_value. The annotation Generator[int, None, None] indicates that the generator yields values of type int, does not accept values when sent, and does not return a value upon completion.