Home > Mobile >  the meaning of this line: username: str | None = None?
the meaning of this line: username: str | None = None?

Time:10-14

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 type str or may be None, and by default it is initialized to None

  • Related