Python·100 questions

How to use Optional, Union, and | (PEP 604)?

Answer
Optional[str] = Union[str, None].
In 3.10+: str | None.
Union[int, str] = int | str.
Prefer | in modern code.
Don't forget about handling None.
Was this answer helpful?

More questions in this topic

Related questions from other topics