On this page
typing.Generator
Prev: typing.FrozenSet | Up: typing | Next: typing.Generic
Rel: typing.AbstractSet, typing.Annotated, typing.Any, typing.assert_never
Ref: docs.python.org — typing.Generator
Tags: #public #python3 #stdlib #typing
Import
import typing
from typing import Generator
Signature & Overview
typing.Generator(*args, **kwargs)
Deprecated alias to collections.abc.Generator.
Usage
from typing import Generator
def squares(n: int) -> Generator[int, None, None]:
for i in range(n):
yield i * i
assert list(squares(3)) == [0, 1, 4]
Gotchas
- Type parameters are
[YieldType, SendType, ReturnType]: Prefercollections.abc.Iterator[int]if you don't use.send()or a return value.
REPL Inspect
from typing import Generator
help(Generator)
Linked from
Related notes
- tuts/python3/stdlib_module/typing/FrozenSet Linked here; links to this page; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/Generic Linked here; links to this page; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/typing/AbstractSet Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/Annotated Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/Any Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/assert_never Linked here; shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/assert_type shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/AsyncContextManager shared tags: #stdlib, #typing
- tuts/python3/stdlib_module/typing/AsyncGenerator shared tags: #stdlib, #typing