Python编程风格,命名
2013-04-01 00:00
330 查看
需要规范一下代码了,参考google的Python风格指导:
http://google-styleguide.googlecode.com/svn/trunk/pyguide.html
文档中还包含了除了命名规范以外的其它东西,为方便阅读,直接把命名规范贴出:
http://google-styleguide.googlecode.com/svn/trunk/pyguide.html?showone=Naming#Naming
module_name, package_name, ClassName, method_name, ExceptionName, function_name, GLOBAL_CONSTANT_NAME, global_var_name, instance_var_name, function_parameter_name, local_var_name.
Names to Avoid
single character names except for counters or iterators
dashes (-) in any package/module name
__double_leading_and_trailing_underscore__ names (reserved by Python)
Naming Convention
"Internal" means internal to a module or protected or private within a class.
Prepending a single underscore (_) has some support for protecting module variables and functions (not included with import * from). Prepending a double underscore (__) to an instance variable or method effectively serves to make the variable or method private to its class (using name mangling).
Place related classes and top-level functions together in a module. Unlike Java, there is no need to limit yourself to one class per module.
Use CapWords for class names, but lower_with_under.py for module names. Although there are many existing modules named CapWords.py, this is now discouraged because it's confusing when the module happens to be named after a class. ("wait -- did I write import StringIO or from StringIO import StringIO?")
Guidelines derived from Guido's Recommendations
http://google-styleguide.googlecode.com/svn/trunk/pyguide.html
文档中还包含了除了命名规范以外的其它东西,为方便阅读,直接把命名规范贴出:
http://google-styleguide.googlecode.com/svn/trunk/pyguide.html?showone=Naming#Naming
Naming
link▽module_name, package_name, ClassName, method_name, ExceptionName, function_name, GLOBAL_CONSTANT_NAME, global_var_name, instance_var_name, function_parameter_name, local_var_name.
Names to Avoid
single character names except for counters or iterators
dashes (-) in any package/module name
__double_leading_and_trailing_underscore__ names (reserved by Python)
Naming Convention
"Internal" means internal to a module or protected or private within a class.
Prepending a single underscore (_) has some support for protecting module variables and functions (not included with import * from). Prepending a double underscore (__) to an instance variable or method effectively serves to make the variable or method private to its class (using name mangling).
Place related classes and top-level functions together in a module. Unlike Java, there is no need to limit yourself to one class per module.
Use CapWords for class names, but lower_with_under.py for module names. Although there are many existing modules named CapWords.py, this is now discouraged because it's confusing when the module happens to be named after a class. ("wait -- did I write import StringIO or from StringIO import StringIO?")
Guidelines derived from Guido's Recommendations
Type | Public | Internal |
---|---|---|
Packages | lower_with_under | |
Modules | lower_with_under | _lower_with_under |
Classes | CapWords | _CapWords |
Exceptions | CapWords | |
Functions | lower_with_under() | _lower_with_under() |
Global/Class Constants | CAPS_WITH_UNDER | _CAPS_WITH_UNDER |
Global/Class Variables | lower_with_under | _lower_with_under |
Instance Variables | lower_with_under | _lower_with_under (protected) or __lower_with_under (private) |
Method Names | lower_with_under() | _lower_with_under() (protected) or __lower_with_under() (private) |
Function/Method Parameters | lower_with_under | |
Local Variables | lower_with_under |
相关文章推荐
- Java编程风格与命名规范整理
- python笔记3:注释命名风格
- python编程风格
- Python命名风格PEP8最佳实践
- python编程风格(python官方PEP8文档)
- [C++][编程风格]C++命名规则
- <<The Pratice of Programming>>编程风格之命名
- Python 编程风格
- python笔记之编程风格大比拼
- Python编程风格
- 编程风格——编程中的命名设计那点事
- [C++][编程风格]C++命名规则
- 关于编程命名规则与代码风格
- [C++][编程风格]C++命名规则
- python高效编程技巧2(如何为元组的每个元素命名,提高程序的可读性)
- python的编程风格
- 一、编程规约 (一)命名风格
- 《Modern Python Cookbook》(Python编程范例)笔记1.2 命名
- Google Python 命名 编程规范 小记录
- python基本语法和编程风格