Http://www.blogjava.net/lincode/archive/2011/02/02/343859.html
Summary: Variables: 1. Variable with _: Indicates that it is a private variable and is only used to indicate that the external class can access the variable 2. Front with two _, followed by two _ variables: Marked is a built-in variable, 3. Capitalize the underlined variable: indicates that the global variable function will not change: 1. Variable with _: marked as a private function, only for marking, 2. A function with two _, followed by two _: a code style marked as a special function Python is described by PEP 8. This document describes all aspects of the Python programming style. Under the conditions of this document, Python code written by different programmers can maintain a similar style to the maximum extent possible. This makes it easy to read and communicate easily between programmers.
1 Variables
Constants: Uppercase and underlined
User_constant for global variables that do not change, use uppercase and underline.
Private variables: Lowercase and a leading underline
There is no private variable in _private_valuepython if you encounter a variable that needs to be protected, use lowercase and a leading underscore. But this is only a convention between programmers, to warn that this is a private variable, the external class does not go to access it. But in fact, the external class can still access this variable.
built-in variables: lowercase, two leading underscores, and two post-underline
__class__ two leading underscores cause variables to be renamed during interpretation. This is to avoid conflicts between built-in variables and other variables. User-defined variables should be strictly avoided in this style. So as not to cause confusion.
2 Functions and methods
In general, lowercase and underscore should be used. But some older libraries use mixed case, that is, the first word is lowercase, then the first letter of each word is capitalized, and the rest is lowercase. But now, lowercase and underscores have become specifications.
Private Methods: Lowercase and a leading underline
def _secrete (self):
Print "Don t test me."
As with private variables, this is not really a private access right. It is also important to note that the generic function does not use two leading underscores (Python's name adaptation feature will work when two leading underscores are encountered). Special functions are mentioned later.
Special methods : Lowercase and two leading underscores, two post-underline
def __add__ (self, Other):
Return int.__add__ (Other)
This style applies only to special functions, such as operator overloading.
function Parameters : Lowercase and underscore, default equals no spaces on both sides
def connect (self, user=none):
Self._user = user
Class 3
Class is always named with the hump format, that is, all words are capitalized with the remaining lowercase letters. The class name should be concise, precise, and sufficient to understand the work done by the class. One common method is to use a suffix that represents its type or attribute, for example:
SQLEngine
Mimetypes
For base classes, you can use a base or Abstract prefix
Basecookie
Abstractgroup
Class UserProfile (object):
def __init__ (self, Profile):
return self._profile = Profile
def profile (self):
Return Self._profile
4 Modules and Packages
In addition to the special module __init__, the module name uses lowercase letters that are not underlined.
If they implement a protocol, LIB is usually used as a suffix, for example:
Import Smtplib
Import OS
Import Sys
5 About parameters
5.1 Do not use assertions to implement static type detection
Assertions can be used to examine parameters, but should not only be static type detection. Python is a dynamic type language, and static type detection violates its design idea. Assertions should be used to avoid calls to functions that are not meaningless.
5.2 Do not abuse *args and **kwargs
The *args and **kwargs parameters can break the robustness of the function. They blur signatures, and code often begins to build small parametric parsers where they shouldn't be.
6 other
6.1 Naming a Boolean element with a has or is prefix
Is_connect = True
Has_member = False
6.2 Naming sequences in plural form
Members = [' user_1 ', ' user_2 ']
6.3 Naming a dictionary with an explicit name
person_address = {' user_1 ': ' Ten Road WD ', ' user_2 ': ' Street Huafu '}
6.4 Avoiding generic names
Names such as list, dict, sequence, or element should be avoided.
6.5 Avoid existing names
Names such as OS, SYS, which already exist, should be avoided.
7 some numbers
Number of rows: PEP 8 is set to 79 columns , which is a bit harsh. Depending on your situation, for example, do not exceed the number of display columns of the editor when full screen. This makes it easy to view the code without moving the horizontal cursor.
A function: Do not exceed 30 lines of code to display in a screen class, you can see the entire function without using a vertical cursor.
A class: Do not exceed 200 lines of code, and do not have more than 10 methods.
Do not exceed 500 lines for a module.
8 Validation Scripts
You can install a PEP8 script to verify that your code style conforms to PEP8.
>>easy_install PEP8
>>pep8-r--ignoire E501 test.py
This command line means repeating the error and ignoring the 501 error (the code is more than 79 lines).
"Go" meaning of the underlined variables and functions in Python