The Way of Operation and Maintenance of Cat_IT Operation and Maintenance Technology Blog_ Operation and Maintenance Learning Communication Blog_DevOps_Automated Operation and Maintenance
As a dynamic language, Python's variable types can change freely.This feature improves code development efficiency, but also increases the difficulty of reading and maintaining code.
Suppose there is a variableis_request_finishedFrom the name, the value of this variable should beTrueperhapsFalseWhen writing code, it was originally defined like this.However, for some reasons, when a value is assigned,is_request_finished = 'True'。At this point, if the unit test of the code is not perfect, thenif is_request_finishedstayis_request_finished = Trueandis_request_finished = 'True'The problem is hidden.But whenis_request_finished = 'False'Because'False'As a non empty stringif is_request_finishedIt is still valid, so that the behavior of the program can be found abnormal.
The type exception of a single variable may be easy to find, but if the variable is placed in a dictionary or list, it will be troublesome.Assuming that a piece of personal information needs to be saved, the following list set dictionary data structure is created:
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
person_list = [{
'name': 'kingname',
'age': 23,
'sex': 'male'
'detail': {
'address': 'xxx',
'work': 'engineer',
'salary': 100000
}
},
{
'name': 'xiaoming',
'age': 65,
'sex': 'male'
'detail': {
'address': 'yyy',
'work': 'pm',
'salary': 0.5
}
}]
This way of development is very fast and convenient, but other people, even developers, will have an impulse to kill themselves when they read the code after a period of time.Because I don't know what is stored in this variable.
There are three common solutions to the above problems.
Type Hints and Variable Annotations
stayPEP 484Type Hints is introduced inPEP 526Variable Annotations is introduced in.It enables Python 3.6 and later Python code to have the ability to "declare" variable types.The "statement" here is quoted because it is for IDE and people.This declaration is invalid for Python interpreter.
Type Hints
PyCharm now supports Type Hints better.For example, the following code:
one
two
three
defupload(url):
print(f'now upload a file to {url}')
returnTrue
Simulate a function to upload a file, and return True after the upload is successful.Receive a parameterurl。Under normal circumstances, thisurlIt should be a string.Then, using Type Hints, the code can be changed to:
one
two
three
defupload(url: str)-> bool:
print(f'now upload a file to {url}')
returnTrue
If it runs directly, its operation effect is shown in the following figure:
Now suppose you pass a variable that is not a string touploadFunction, PyCharm will prompt that there is a problem with the type, as shown in the following figure:
However, the prompt is returned to the prompt, and there is no problem in forced operation.This indicates that Type Hints is mainly for IDE and people, and the interpreter does not care about the incorrect type.
If you modify the return value of this function, it will not returnTrueperhapsFalse, PyCharm will also issue a warning:
For Variable Annotations, as shown in the figure below, PyCharm can't tell you that the variable type is wrong at present, but people can still help when reading code.
In addition to this writing method, Variable Annotations also supports writing types in annotations, as shown in the following figure:
Although PyCharm cannot provide good hints, a third-party library can be usedmypyTo perform a static check on the code, the operation effect is shown in the following figure. It can be found that the type assigned is inconsistent with the declared type (expression has type "str", variable has type "bool", expression type is "str", variable type is "bool").
Mark the variable type in docstring, as shown in the following figure:
This writing method can be used to prompt the situation of each variable in a function or a class.But the level of detail depends on the developer's patience in writing this comment clearly.