Typing module

Since python3.5, PEP484 has introduced type annotations for python (type hints)

  • Type check to prevent run-time parameter and return value type, variable type mismatch.

  • As an add-on to the development document, it is easy to pass in and return parameter types when invoked by the consumer.

  • This module will not affect the operation of the program, will not report a formal error, only to remind.

    Pycharm currently supports typing checks, which are yellow for parameter type errors.

    Commonly used type

  • Int,long,float: integer,long integer,float

  • Bool, STR: Boolean, string

  • List, Tuple, Dict, Set: List, Tuple, dictionary, Set

  • Iterable: Iterable type,Iterator type

  • Generator: Generator type

Base type specification

from typing import List.Dict.Tuple.Union
# integer
num:int = 100
# string
data:str = "200"
# Boolean value
bo:bool = True
# list
data_list:List[str] = ["1"."2"."3"]
# dictionary
data_dict:Dict[str.str] = {"name":"xiaoming",}
# tuples [limit data and types]
data_tuple:Tuple[int.int.bool] = (1.2.True)

# join type
U1 = Union[str.int] # must be a string or an integer
data_union1:U1 = "20"
data_union2:U1 = 200
data_union3:U1 = [1.2.3]  # This does not meet the requirements, there is a yellow prompt

def test(a:int, b:str) - >str:
    print(a, b)
    return 1000

if __name__ == '__main__':
    test('test'.'abc')

"" Function test, a:int specifies that the input parameter a is of type int, b: STR b is of type STR, and -> STR returns type SRT. As you can see, in the method we eventually return an int and pyCharm will be warned; When we call this method, the argument a is a string, and we get a warning; It is important to note, however, that PyCharm is a warning, but it is not an error. Python is by nature a dynamic language.
Copy the code
from typing import List
Vector = List[float]

def scale(scalar: float, vector: Vector) -> Vector:
    return [scalar * num for num in vector]

Pass the data as long as it is in a Vector format.
new_vector = scale(2.0[1.0, -4.2.5.4])
Copy the code
from typing import Dict.Tuple.Sequence

ConnectionOptions = Dict[str.str]
Address = Tuple[str.int]
Server = Tuple[Address, ConnectionOptions]


if __name__ == '__main__':
    def broadcast_message1(message: str, servers: Sequence[Server]) - >None:
        print(message)
        print(servers)

    broadcast_message1(message="hello", servers=[(("127.0.0.1".8080), {"username": "xiaoming"}), (("127.0.0.2".8080), {"username": "xiaohong"})])


    def broadcast_message2(message: str, servers: Sequence[Tuple[Tuple[str.int].Dict[str.str]]]) - >None:
        print(message)
        print(servers)

    broadcast_message2(message="hello", servers=((("127.0.0.1".8080), {"username": "xiaoming"}), (("127.0.0.2".8080), {"username": "xiaohong"})))

    Note here that the tuple type is special because it is immutable. So, when we specify Tuple[STR, STR], we can only pass in length 2 and all elements in the Tuple are of type STR """
Copy the code