The usage of typing.union in Python

1. Python can pass two kinds of parameters and return multiple values

Usually, a parameter and return value can only be of one type. In C/C + +, Java and golang, it is impossible to return two types, or pass parameters to use two types, but it is possible in Python.

def mytest(a:str or int)->str or int:
  return a*2

2. Using Uinon in Python

from typing import Union
def mytest(a:Union[str,int])->Union[str,int]:
  return a*2


Read More: