Optional (typing)
noun
say it: “OP-shuh-nul” /ˈɒpʃənəl/ or “TYPE-ing” /ˈtaɪpɪŋ/
A type hint meaning a value may be that type or None, written Optional[str] or str | None.
What does Optional mean in a type hint?
Optional[str] (or str | None) means the value is either that type or None.
def find_customer(email: str) -> str | None:
customers = {"ana@example.com": "Ana"}
return customers.get(email) # None if not found
In a sentence
- “An Optional field with a default of None can be left out of the JSON.”
- “The phone field is Optional, so an application without one is fine.”
Level: Freelancer · More Python terms at this level
Updated