Union Type
noun
say it: “YOON-yun TYP” /ˈjunjən taɪp/
A type hint saying a value may be one of several types, written with a vertical line, such as int | str or float | None.
What is a union type?
A union type (int | str) says a value can be one of several types.
def format_id(order_id: int | str) -> str:
return f"#{order_id}"
print(format_id(42)) # #42
print(format_id("A-17")) # #A-17
In a sentence
- “The union type int | str lets format_id take 42 or "A-17".”
- “find_order returns a union type, Order | None, because the order may not exist.”
Level: Freelancer · More Python terms at this level
Updated