Python is a weakly typed language, and many times we may not know the type of function arguments or return values. In order to facilitate gongcheng lion to read the source code, so it is important to define the data type of the parameters when defining the function. In fact, some great people have helped us develop this, we just need to use, it is the typing package.
- typing.Any
Any is a special type that can represent all types, similar to object, where all types are subclasses of Object.
- typing.Union
A union is a union type. A union [X, Y] is either of type X or Y
- typing.Optional
Optional specifies an optional, empty or already declared type optional [X] is equivalent to Union[X, None]
- typing.Tuple
Tuple is a tuple type, tuple [int, STR, float]
- typing.Dict
Dict[STR, int] Mapping[STR, int]
- typing.Set
Set is a set type set [int] AbstractSet[int]
- typing.TypeVar
Height = typevar (‘ height ‘, int, float, None) typevar (‘ height ‘, int, float, None)
- typing.Iterable
Iterable is an iterable type
- typing.Callable
Callable[[int], None] is a function that takes an int argument and returns None
- typing.NewType
UserId = newType (‘UserId’, int)