I'm reading the documentation of a python libarary and I got blocked in this code, I coudn't understand the meaning :
class TokenData(BaseModel):
username: str | None = None
what's the effect of ' | None = None ' on the attribute username ?
CodePudding user response:
It's a union type hint, similar to:
class TokenData(BaseModel):
username: Union[str, None] = None
It basically means:
username
may be any value of typestr
or may beNone
, and by default it is initialized toNone