Chinaunix首页 | 论坛 | 博客
  • 博客访问: 273875
  • 博文数量: 40
  • 博客积分: 1807
  • 博客等级: 上尉
  • 技术积分: 350
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-03 15:42








2010-11-08 13:39:38

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 (小写+下划线)
• 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.
• single_trailing_underscore_: used by convention to avoid conflicts with Python
keyword, e.g.
Tkinter.Toplevel(master, class_='ClassName')
• __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__
• __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.

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.,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.)

阅读(4789) | 评论(1) | 转发(0) |

chinaunix网友2010-11-08 15:36:37

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: