How python uses the typing module to enhance the readability of the code
This article mainly shows you "python how to use the typing module to enhance the readability of the code", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "python how to use the typing module to enhance the readability of the code" this article.
I. description of requirements
Programming without type hints always feels inconvenient, but luckily there is a typing module built in after python3.5.
The typing module annotates functions and variable types.
However, the Python runtime does not enforce function and variable type annotations, but these annotations can be used for third-party tools such as type checkers, IDE, static checkers, and so on.
Official website:
Typing-Type hint support-Python 3.10.1 documentation
Https://docs.python.org/zh-cn/3/library/typing.html
II. Actual combat exercises
1. Experience the annotation function
As shown below, if you define a variable type without assigning a value, you will be prompted for the assignment, but the run will not report an error.
From typing import AbstractSetfrom typing import Dictfrom typing import Generatorfrom typing import Listfrom typing import Mappingfrom typing import Optionalfrom typing import Tuplefrom typing import TypeVarfrom typing import Union list_001: list [int] list_001 = 123
2. The use of List and Dict
# def test_List (num: int)-> List [int]: return [num, bool (num), str (num)] # as long as one matches the specified type No prompt will be given for the use of # Dict: def test_Dict (num: int)-> Dict [str,int]: # return {"num": "num", 1: "str (num)", "str (num)": 1} # as long as there is one that matches the specified type, no prompt will be given # return {1: 3} # if key is 1 You will be prompted that key should be str # return {"1": 3} print (test_Dict (5))
3. The use of Union
# def test_List (num: int)-> [int or str or bool]: return [num, bool (num), str (num)] # as long as one matches the specified type No hint will be given for the use of # Dict and Union def test_Dict (num: int)-> Dict [str,Union [int, str, bool]]: # return {"num": "num", 1: "str (num)", "str (num)": 1} # as long as one matches the specified type, no prompt will be given # return {1: 3} # if key is 1 You will be prompted that key should be str # return {"1": 3} print (test_List (1)) print (test_Dict (5))
4. The use of Optional
This parameter can be empty or a declared type, that is, Optional [X] is equivalent to Union [X, None]
# use of Dict and Optional def test_Dict (num: int)-> Dict [str,Optional [str]]: # return {"num": "num", 1: "str (num)", "str (num)": 1} # as long as one matches the specified type, no prompt will be given # return {1: 3} # if key is 1 You will be prompted that key should be str # return {"1": 3} def test_Dict01 (num: int)-> Dict [str,Optional [int]]: # return {"1": None}
5. The use of Tuple
Consistent with the list, tuple generics require that the types of each location correspond one to one
# Optional's use of def test_Dict (num: int)-> Dict [int,Tuple [int,int]]: # return {1: (1)} # return {1: (1)} # the above is all the content of the article "how python uses the typing module to enhance the readability of code". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!