DEV Community

Vaishnvee Shinde
Vaishnvee Shinde

Posted on

Python 3.10 Feature

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)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more