home page » programing language » Three ways to remember variable types

Three ways to remember variable types

 

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 variable is_request_finished From the name, the value of this variable should be True perhaps False When 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, then if is_request_finished stay is_request_finished = True and is_request_finished = 'True' The problem is hidden. But when is_request_finished = 'False' Because 'False' As a non empty string if is_request_finished It 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

stay PEP 484 Type Hints is introduced in PEP 526 Variable 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
def upload (url) :
print(f 'now upload a file to {url}' )
return True

Simulate a function to upload a file, and return True after the upload is successful. Receive a parameter url Under normal circumstances, this url It should be a string. Then, using Type Hints, the code can be changed to:

one
two
three
def upload (url: str) -> bool:
print(f 'now upload a file to {url}' )
return True

If it runs directly, its operation effect is shown in the following figure:

Now suppose you pass a variable that is not a string to upload Function, 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 return True perhaps False , PyCharm will also issue a warning:

For the official documents of Type Hints, please refer to: typing — Support for type hints

Variable Annotations

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 used mypy To 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").

For more usage of Variable Annotations, see: Syntax for Variable Annotations
For Mypy, please refer to its official documentation: Mypy documentation

docstring

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.

Original link: Three ways to remember variable types , Please indicate the source for reprinting!

fabulous three