一,包名、模塊名、局部變量名、函數名
全小寫+下划線式駝峰
example:this_is_var
二,全局變量
全大寫+下划線式駝峰
example:GLOBAL_VAR
三,類名
首字母大寫式駝峰
example:ClassName()
四,關於下划線
- 以單下划線開頭,是弱內部使用標識,from M import * 時,將不會導入該對象(python 一切皆對象)。
 - 以雙下划線開頭的變量名,主要用於類內部標識類私有,不能直接訪問。模塊中使用見上一條。
 - 雙下划線開頭且雙下划線截尾的命名方法盡量不要用,這是標識
 
example for 1:
module_1 模塊中定義變量 var_1, _var_2, __var_3
#module_1 var_1 _var_2 __var_3
module_2 模塊中代碼如下:
1 #module_2_error 2 ''' 3 以下划線開頭的變量不會別導入 4 ''' 5 from module_1 import * 6 7 print var_1 8 print _var_2 #將報錯 9 print __var_3 #將報錯
執行到第6,7行將會報錯,因為凡是以下划線開頭的對象都不會被導入。
既然是弱內部使用標識,就還是有使用辦法的,只需單獨導入即可:
1 #module_2_solution 2 3 from module_1 import * # 導入所有的不是下划線開頭的對象 4 5 from module_1 import _var_2, __var_3 # 顯式導入下划線開頭的對象 6 7 print var_1 8 print _var_2 # 不會報錯 9 print __var_3 # 不會報錯
example for 2:
1 #module_error 2 ''' 3 雙下划線開頭的變量不能被直接訪問 4 ''' 5 6 class MyClass(): 7 def __init__(self): 8 self.var_1 = 1 9 self._var_2 = 2 10 self.__var_3 = 3 11 12 if __name__=="__main__": 13 obj = MyClass() 14 print obj.var_1 15 print obj._var_2 16 print obj.__var_3 # 這里將會出錯
#module_solution ''' 需要定義函數來獲取雙下划線開頭的變量 ''' class MyClass(): def __init__(self): self.var_1 = 1 self._var_2 = 2 self.__var_3 = 3 def get_var_3(self): return self.__var_3 if __name__=="__main__": obj = MyClass() print obj.var_1 print obj._var_2 print obj.get_var_3() # 不會再報錯
四,其他要注意的
- 不要像c等語言里面一樣去用開頭字母標識變量類型(如 iValue),因為python在解釋的時候才確定類型。
 - 因為異常也是一個類,所以遵守類的命名規則。此外,如果異常實際上指代一個錯誤的話,應該使用“Error”做后綴。
 - 命名應當盡量使用全拼寫的單詞,縮寫的情況有如下兩種:常用的縮寫,如XML、ID等,在命名時也應只大寫首字母,如XmlParser。命名中含有長單詞,對某個單詞進行縮寫。這時應使用約定成俗的縮寫方式。例如:function 縮寫為 fn, text 縮寫為 txt, object 縮寫為 obj, count 縮寫為 cnt, number 縮寫為 num 等。
 - 類實例方法第一個參數使用self, 類方法第一個參數使用cls
 
五,最后,把一些英文說明作為備注
Package and Module Names
Modules should have short, all-lowercase names. Underscores can be used
in the module name if it improves readability. Python packages should
also have short, all-lowercase names, although the use of underscores is
discouraged.
Since module names are mapped to file names, and some file systems arecase insensitive and truncate long names, it is important that module
names be chosen to be fairly short -- this won't be a problem on Unix,
but it may be a problem when the code is transported to older Mac or
Windows versions, or DOS.
Class Names
Almost without exception, class names use the CapWords convention.
Classes for internal use have a leading underscore in addition.
Exception Names
Because exceptions should be classes, the class naming convention
applies here. However, you should use the suffix "Error" on your
exception names (if the exception actually is an error).
Global Variable Names
(Let's hope that these variables are meant for use inside one module
only.) The conventions are about the same as those for functions.
Modules that are designed for use via "from M import *" should use the
__all__ mechanism to prevent exporting globals, or use the older
convention of prefixing such globals with an underscore (which you might
want to do to indicate these globals are "module non-public").
Function Names
Function names should be lowercase, with words separated by underscores
as necessary to improve readability.
mixedCase is allowed only in contexts where that's already the
prevailing style (e.g. threading.py), to retain backwards compatibility.
Function and method arguments
Always use 'self' for the first argument to instance methods.
Always use 'cls' for the first argument to class methods.
If a function argument's name clashes with a reserved keyword, it is
generally better to append a single trailing underscore rather than use
an abbreviation or spelling corruption. Thus "print_" is better than
"prnt". (Perhaps better is to avoid such clashes by using a synonym.)
Method Names and Instance Variables
Use the function naming rules: lowercase with words separated by
underscores as necessary to improve readability.
Use one leading underscore only for non-public methods and instance
variables.
To avoid name clashes with subclasses, use two leading underscores to
invoke Python's name mangling rules.
Python mangles these names with the class name: if class Foo has an
attribute named __a, it cannot be accessed by Foo.__a. (An insistent
user could still gain access by calling Foo._Foo__a.) Generally, double
leading underscores should be used only to avoid name conflicts with
attributes in classes designed to be subclassed.
Note: there is some controversy about the use of __names (see below).
Constants
Constants are usually defined on a module level and written in all
capital letters with underscores separating words. Examples include
MAX_OVERFLOW and TOTAL.
