Skip to content Skip to sidebar Skip to footer

Optional Type Annotation. Using Value After Checking If Is None?

I was writing some code with type annotations in python. I have problem with Optional type. For example for a code like this: maybe_number : Optional[int] = ... # definition if ma

Solution 1:

Equality doesn't actually tell you much about an objects type, because the two objects could have __eq__ methods that allow them to be equal but of different, even unrelated, types. You can check if maybe_number is the value None by using identity comparison:

if maybe_number is None:
    ...
else:
    number : int = maybe_number

Post a Comment for "Optional Type Annotation. Using Value After Checking If Is None?"