1. Current Problem
With the current Python version, if you want to use type hints for
function that has a parameter which can recive value of different
types, you had to use Union type.
Something like this:
def some_funcion(flexible_parameter: Union[int, string]) -> Union[int, string]: return flexible_parameter
2. Solution Python 3.10 Feature
It introduces new union operand – |. What this operand says is that certain parameter can be either Type 1 either Type 2.
Now, the previous function can be written like this:
def some_funcion(flexible_parameter: int | string) -> int |
string: return flexible_parameter
Top comments (0)