CKTIS / Python
typing.Union
Označuje hodnoty aspoň jedného členského typu.
Verzia: Python 3.14; repository image, production runtime not attested
Syntax
Union[int, str]Označuje hodnoty aspoň jedného členského typu.
V Pythone 3.14 sú typing.Union a types.UnionType tá istá trieda, duplicity sa odstránia a poradie nemení rovnosť.
Pri isinstance môže skoršia zhoda obísť list[int], ale jeho vyhodnotenie vyvolá TypeError.
Príklady
import types
from typing import Union
u = Union[int, str, int]
print(Union is types.UnionType, u == (str | int))
mixed = int | list[int]
print(isinstance(2, mixed))
try:
isinstance([], mixed)
except TypeError:
print("parameterized member")
Výsledok
True True True parameterized member
Pri isinstance môže skoršia zhoda obísť list[int], ale jeho vyhodnotenie vyvolá TypeError.