How to write high-quality Pythonic style code

Every language has its own programming style . For example, various cuisines, Cantonese, Sichuan, and Huaiyang cuisines each have their own unique style. Python developers use the term Pythonic to describe that kind of conformity. Python-style code. This pythonic- style code is neither a very strict specification nor a rule imposed by the editor on the developer. It is a guide specified by everyone in the process of the python language , a kind of collaborative work. A gradually formed habit. The purpose is to be intuitive, concise and easy to read.

1. Avoid using only upper and lower case to distinguish different objects. For
example, a is a numeric variable, and A is a String type. Although it is easy to distinguish the meaning of the two in the encoding process, it is of no use and it will not be used for others. How much convenience is brought by people who read the code.

2, avoid the use of confusing names
do not use the built-in function name to represent the name of the other meanings, not built using similar element, list, dict such as the case of new data types as variable names. Do not use o (character O 's Lower case, it’s easy to think that it is the value 0), the long one is too similar, 1 ( the lower case of the letter L is also easy to confuse the value 1), the
variable name is best connected with the problem you want to solve.

3. Don't be afraid of long variable names.
Sometimes, in order to make the program easier to understand and read, longer variable names are necessary . Some students said that long variable names have to be typed many times. It doesn't matter what to do. Now Many editors have automatic code prompts, which is not a problem. For
example, the following person_info is much more readable than pi:

person_info={'name':'zhang san','IDCard':'200123','address':'Num 100,Xihu Road'}

4. Look at the source code more and learn other people's excellent code styles. It
is a faster learning process. Especially the more recognized Pythonic code in the industry, such as the more famous Flask or requests in the web framework, the classic one is better Understand the essence of pythonic style code.

5. The use of an artifact, such as PEP8
This is a guide for Python coding style, full name "Python Enhancement Proposal # 8" which lists many of the details of the layout contains some code, comments, naming requirements
(if Students who are interested in PEP8 can leave a message, I will open a separate chapter to talk about the variables, functions, parameters, classes, modules and packages in PEP8). If you can follow these principles, it's easy to get pythonic code.

Need to download a pep8 first, press pip install -U pep8.
Then use it to detect some of your own code, and it's clear at a glance.

$ pep8 --first MyPython.py
MyPython.py:1:36: E231 missing whitespace after ','
MyPython.py:31:6: E225 missing whitespace around operator
MyPython.py:118:80: E501 line too long (85 > 79 characters)
MyPython.py:120:1: W191 indentation contains tabs
MyPython.py:123:38: W291 trailing whitespace
MyPython.py:141:42: E203 whitespace before ':'
MyPython.py:144:57: E202 whitespace before ')'
MyPython.py:150:1: E303 too many blank lines (5)
MyPython.py:306:35: W292 no newline at end of file

Guess you like

Origin blog.51cto.com/15009341/2553631