Python 定名规范
添加时间:2013-6-13 点击量:
一,包名、模块名、局部变量名、函数名
全小写+下划线式驼峰
example:this_is_var
二,全局变量
全大写+下划线式驼峰
example:GLOBAL_VAR
三,类名
首字母大写式驼峰
example:ClassName()
四,关于下划线
- 以单下划线开首,是弱内部应用标识, 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 module_1 import
6
7 print var_1
8 print _var_2 #将报错
9 print __var_3 #将报错
履行到第6,7行将会报错,因为凡是以下划线开首的对象都不会被导入。
既然是弱内部应用标识,就还是有应用办法的,只需零丁导入即可:
1 #module_2_solution
2
3 module_1 import # 导入所有的不是下划线开首的对象
4
5 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 are
case insensitive and long names, it is important that module
names be chosen to be fairly short -- this wont 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
(Lets 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 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 thats 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 arguments 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 Pythons 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.
彼此相爱,却不要让爱成了束缚:不如让它成为涌动的大海,两岸乃是你们的灵魂。互斟满杯,却不要同饮一杯。相赠面包,却不要共食一个。一起歌舞欢喜,却依然各自独立,相互交心,却不是让对方收藏。因为唯有生命之手,方能收容你们的心。站在一起却不要过于靠近。—— 纪伯伦《先知》
一,包名、模块名、局部变量名、函数名
全小写+下划线式驼峰
example:this_is_var
二,全局变量
全大写+下划线式驼峰
example:GLOBAL_VAR
三,类名
首字母大写式驼峰
example:ClassName()
四,关于下划线
- 以单下划线开首,是弱内部应用标识, 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 module_1 import
6
7 print var_1
8 print _var_2 #将报错
9 print __var_3 #将报错
履行到第6,7行将会报错,因为凡是以下划线开首的对象都不会被导入。
既然是弱内部应用标识,就还是有应用办法的,只需零丁导入即可:
1 #module_2_solution
2
3 module_1 import # 导入所有的不是下划线开首的对象
4
5 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 long names, it is important that module
names be chosen to be fairly short -- this wont 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
(Lets 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 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 thats 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 arguments 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 Pythons 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.
彼此相爱,却不要让爱成了束缚:不如让它成为涌动的大海,两岸乃是你们的灵魂。互斟满杯,却不要同饮一杯。相赠面包,却不要共食一个。一起歌舞欢喜,却依然各自独立,相互交心,却不是让对方收藏。因为唯有生命之手,方能收容你们的心。站在一起却不要过于靠近。—— 纪伯伦《先知》