白皮书下载
Python encoding rules
1. Variables
Constants: uppercase and underline
USER_ CONSTANT
Private variables: lowercase and a leading underscore
_ private_ value
There is no private variable in Python. If you encounter a variable that needs to be protected, use lowercase and a leading underscore. But this is just a convention between programmers to warn that this is a private variable and external classes should not access it. But in fact, external classes can still access this variable.
Built in variables: lowercase, two leading underscores and two trailing underscores
__ class__
Two leading underscores cause the variable to be renamed during interpretation. This is to avoid conflicts between built-in variables and other variables. Strictly avoid this style for user-defined variables. To avoid confusion.
2. Functions and methods
In general, lowercase and underline should be used. But some older libraries use mixed case, that is, the first word is lowercase, and then the first letter of each word is uppercase and the rest is lowercase. But now lowercase and underscore have become the norm.
Private methods: lowercase and a leading underscore
def_ secrete(self):
print"don't test me."
Like private variables, this is not really private access. It should also be noted that general functions do not use two leading underscores (Python's name adaptation feature will work when two leading underscores are encountered). Special functions will be mentioned later.
Special methods: lowercase and two leading underscores, two trailing underscores
def__ add__(self,other):
returnint.__ add__(other)
This style applies only to special functions, such as operator overloading.
Function parameters: lowercase and underline. The default value is equal, and there is no space on both sides of the equal sign
defconnect(self,user=None):
self._ user=user
3. Class
Classes are always named in Hump format, that is, all words are capitalized and the rest are lowercase. The class name should be concise, precise, and sufficient to understand what the class does. A common method is to use a suffix indicating its type or characteristic, for example:
SQLEngine
MimeTypes
For base classes, you can use a base or abstract prefix
BaseCookie
AbstractGroup
classUserProfile(object):
def__ init__(self,profile):
returnself._ profile=profile
defprofile(self):
returnself._ profile
4. Modules and packages
Except for special modulesinitAll module names use lowercase letters without underscores.If they implement a protocol, they usually use lib as the suffix, for example:
import smtplib
import os
import sys
5. About parameters
•Do not use assertions to implement static type detectionAssertions can be used to check parameters, but not just static type detection. Python is a dynamic type language, and static type detection violates its design idea. Assertions should be used to prevent functions from being called meaninglessly.
•Don't abuse * args and**kwargs*Args and**The kwargs parameter may undermine the robustness of the function. They obscure signatures, and code often starts building small parameter parsers where they shouldn't.
6. Others
•Use the has or is prefix to name Boolean elements
is_ connect = True
has_ member = False
•Name the sequence in the plural
members = ['user_1', 'user_2']
•Naming dictionaries with explicit names
person_ address = {'user_1':'10 road WD', 'user_2' : '20 street huafu'}
•Avoid common namesNames such as list, dict, sequence, or element should be avoided.
•Avoid existing namesNames that already exist for systems such as OS and sys should be avoided.
7. Some figures
Number of rows and columns: PEP 8 stipulates 79 columns, which is a little harsh. According to your own situation, for example, do not exceed the number of columns displayed by the editor when the screen is full. This makes it easy to view the code without moving the horizontal cursor.
A function: no more than 30 lines of code can be displayed in a screen class. You can see the whole function without using a vertical cursor.A class: no more than 200 lines of code and no more than 10 methods.No more than 500 lines per module.
8 validation script
You can install a pep8 script to verify whether your code style conforms to pep8.
>& gt; easy_ install pep8
>& gt; pep8 -r --ignoire E501 Test.py




