What is Union and Collection in class definitions?

Hey, I’m just jumping into the source code and there is alot of Union(float,tensor…) there. What does Union mean? There is also Collection. What is that?

From my understanding Union[type1, type2] would mean that a parameter can be of either types.

So for instance for the function:

def add_two_num(num1:Union[int, float], num2:Union[int, float]):
    print(num1 + num2)

The above function would accept either type for both numbers. (Of course this is unnecessary as python would do that anyway.)

It is useful because it also explicitly says that the parameters cannot be anything other than those types.

I also have a question related to this. Where is Union[] actually defined?

Making a plain python script and using Union[] gives the following error.

raceback (most recent call last):
  File "c:/Users/Danny/Desktop/throwaway.py", line 3, in <module>
    def add_one_to_num(number:Union[int, float]):
NameError: name 'Union' is not defined

What needs to be imported?

Its a part of python3

I’m running python 3.8.1 but its still undefined…

from typing import Union is what you need.
FYI: You need not use Union and do the following too:
def add_one_to_num(number:(int, float)):
Hope that helps :slightly_smiling_face: