Now that you can write a longer, more complex Python program, it's time to discuss the coding style . Most languages can be written (or, more clearly, formatted ) in several different styles. Some are better read than others. It's a good idea to make your code easier for others to read, and it's helpful to develop a good coding style.
For Python, PEP 8 introduces style guidance that most projects follow. It gives a highly readable, visually-friendly coding style. Every Python developer should read and most of the points will help you:
-
Use 4 spaces to indent rather than TAB.
Between small indents (which can be nested deeper) and large indents (easier to read), 4 spaces are a good compromise. TAB has caused some confusion, preferably deprecated.
-
Wrap the line to make sure it doesn't exceed 79 characters.
This helps small-screen users to read, or allows large displays to display several code files side-by-side.
-
Use empty lines to separate functions and classes, and large chunks of code in functions.
-
If possible, comment exclusive line
-
Working with document Strings
-
Place the space on either side of the operator and behind the comma, but no space is added to the brackets: a = f (1, 2) + g (3, 4) .
-
Uniform functions and class naming.
The recommended class names are named with the hump , and the function and method names are underlined in lowercase _ and _ . Always use self as the first parameter of the method (for knowledge of classes and methods see First Class ).
-
Do not use fancy coding if the purpose of your code is to be in an internationalized environment. Python by default, UTF-8, even ordinary ASCII always works best.
-
Also, do not use identifiers that are non-ASCII characters unless the code is read or maintained in a different language.
Python coding style