1. Strictly use indentation to embody the logical affiliation of the code.
Python's indentation of the code is a hard requirement, which must always be noted. If the indentation of a piece of code is incorrect, then the whole program is wrong, either the syntax error cannot be executed, or the logic error results in the error, and checking for such an error can take a lot of time.
2. Each import statement imports only one module, preferably in the order of standard libraries, extension libraries, custom libraries, and so on.
Try to avoid importing the entire library, preferably by importing only the objects that you really need to use, which makes the program run faster.
3. It is best to add a blank line after each class, function definition, and a complete function code, add a space on each side of the operator, and add a space after the comma.
Code written in accordance with such specifications is loosely laid out and easier to read. Whether it is the indentation of the first one, or the empty lines and spaces discussed here, the main point is to improve the readability of the code. A little bit of an exception is that in a normal assignment expression both sides of the equals sign add a space, but in the definition of a function's default value parameter and when calling a function using the keyword argument, it is not generally necessary to add spaces on both sides of the equal sign of the parameter assignment. In this way, it is tight in the loose to improve the readability of the code.
4. Try not to write long sentences.
If the statement is too long, consider splitting it into shorter statements to make the code more readable. If the statement is indeed too long to exceed the screen width, it is best to use the continuation character (line continuation character) "\" or use parentheses to enclose multiple lines of code as a statement.
5. Although the Python operator has a clear precedence, it is recommended to use parentheses in the appropriate place for complex expressions to make the affiliation and order of the various operations more explicit.
6. Make the necessary comments about the critical code and important business logic code.
Statistics show that a well-readable program should contain about 30% or more comments. There are two common types of annotations in Python: #和三引号. #用于单行注释, three quotation marks are often used for annotations of a large section of descriptive text.
7. Make the best possible balance between development speed and operating speed.
The built-in objects run the fastest, the standard library runs second, the expansion libraries written in C or Fortran run faster, and the plain Python extensions tend to be slower. Therefore, when you develop a project, you should prioritize the use of Python built-in objects, and then consider using the objects provided by the Python standard library, and finally consider using a third-party extension library. However, sometimes only the built-in and standard library objects are used, and it is probably not possible to directly meet the needs. There are two options: one is to write code using built-in objects and standard library objects for specific logic, and the other is to use the appropriate extension library objects. The balance between the complexity of the business logic and the requirements for the speed of operation will ultimately depend on how the trade-offs are.
8. Choose the most suitable data type according to the operation characteristics to improve the operation efficiency of the program.
If you define some data for frequent traversal, it is best to prioritize tuples or collections. If you need to test frequently whether an element exists in a sequence and does not care about its location, try to use a dictionary or a collection. The time complexity of the in operation for lists and tuples is linear, and for collections and dictionaries it is a constant level, which is almost irrelevant to the size of the problem. Of all the built-in data types, the list is the most powerful, but the overhead is also large, running the slowest and should be used with caution. As a recommendation, you should prioritize the use of collections and dictionaries, followed by tuples, and finally consider lists and strings.
9. Take full advantage of the lazy evaluation characteristics of relational operators and logical operators and and OR, and rationally organize the order of multiple conditions in conditional expressions to reduce unnecessary computation.
10. Make full use of the lazy calculation characteristics of the generator object or similar iterative object, avoid converting it to list, tuple and other types, so as to reduce the memory consumption and reduce the space complexity.
11. Reduce the extraneous calculation in the inner cycle, as far as possible to the outer layer extraction.
There are a lot of mature tools that can check the norms of Python code, such as PEP8, Flake8, Pylint, etc. You can use Pip to install the Pep8 tool, and then use the command PEP8 test.py to check the normalization of Python code in the test.py file. Pep8 commonly used optional parameters are--show-source 、--first 、--show-pep8 and so on. Flake8 combines the features of Pyflake and PEP8 to check for more content, prioritize the use, use PIP install Flake8 to install directly, and then use the command Flake8 test.py to check the code in the test.py. You can also use Pip to install Pylint, and then use the command line tool Pylint or visualizer Pylint-gui to check the normalization of the program.
Python programming specifications and code optimization recommendations