Naming Conventions
The naming conventions of Python’s library are a bit of a mess, so we’ll never get this completely consistent—nevertheless, here are the currently recommended naming standards. New modules and packages (including third party frameworks) should be written to these standards, but where an existing library has a different style, internal consistency is preferred.
Descriptive: Naming Styles
There are a lot of different naming styles. It helps to be able to recognize what naming style is being used, independently from what they are used for.
The following naming styles are commonly distinguished:
• b (单个小写字母)
• B (单个大写字母)
• lowercase (小写)
• lower_case_with_underscores (小写+下划线)
• UPPERCASE (大写)
• UPPER_CASE_WITH_UNDERSCORES (大写+下划线)
• CapitalizedWords (or CapWords, or CamelCase—so named because of the bumpy
look of its letters8). This is also sometimes known as StudlyCaps. (单词首字母大写,通常缩写词全部字母大写如,HTTPServerError)
In addition, the following special forms using leading or trailing underscores are recognized (these can generally be combined with any case convention):
下面的一些特别的格式是使用前缀或后缀下划线的方式来区分的。
• _single_leading_underscore: weak “internal use” indicator. E.g. from M import *
does not import objects whose name starts with an underscore.
前缀单下划线:弱的内部使用标示。import语句无法将该内部使用对象引入。
• single_trailing_underscore_: used by convention to avoid conflicts with Python
keyword, e.g.
Tkinter.Toplevel(master, class_='ClassName')
后缀单下划线:被用来同Python保留字进行区分。
• __double_leading_underscore: when naming a class attribute, invokes name mangling (inside class FooBar, __boo becomes _FooBar__boo; see below).
前缀双下划线:命名类私有变量,将变量名前面加入类名。
• __double_leading_and_trailing_underscore__: “magic” objects or attributes that live in user-controlled namespaces. E.g. __init__
前后双下划线:“magic”对象或者是用户命名空间中的属性。
• __import__ or __file__. Never invent such names; only use them as documented.
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.
模块应该使用短的,全部小写字母的名字。 如果下划线能够提高可读性则也可以使用。Python的包也要使用短的小写的名字,但是下划线则不鼓励使用。
Since module names are mapped to file names, and some file systems are case 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.
When an extension module written in C or C++ has an accompanying Python module that provides
a higher level (e.g. more object oriented) interface, the C/C++ module has a leading underscore (e.g.
_socket).
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).
异常命名
因为异常也是类,所以类的命名习惯在这里也适用。不同的是,如果异常实际上是个错误,则需要在异常名字的后面使用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.
函数命名
函数名是小写字母,必要时使用下划线区分单词以提高可读性。
mixedCase(首字母小写,后面以单词首字母大写分割单词)只适用于保持风格以及向下兼容。
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.)
函数和方法的参数
使用self作为实例函数的第一个参数,使用cls作为类函数的第一个参数。
如果函数参数的名字与保留字冲突,最好是添加下划线后缀,而不是使用缩写或者造字。
阅读(5018) | 评论(1) | 转发(0) |