python的编码规范【摘】
模块名:
小写字母,单词之间用_分割
ad_stats.py
包名:
和模块名一样
类名:
单词首字母大写
AdStats
ConfigUtil
全局变量名(类变量,在java中相当于static变量):
大写字母,单词之间用_分割
NUMBER
COLOR_WRITE
普通变量:
小写字母,单词之间用_分割
this_is_a_var
实例变量:
以_开头,其他和普通变量一样
_price
_instance_var
普通函数:
和普通变量一样:
get_name()
count_number()
ad_stat()
私有函数(外部访问会报错):
以__开头(2个下划线),其他和普通函数一样
__get_name() 这里有人建议一个下划线,有人建议两个下划线。下面得到一个答复觉得比较有说服力一些:
“在python中定义私有变量只需要在变量名或函数名前加上 "__" (两个下划线),那么这个函数或变量就会成为私有的了。在内部,python使用一种 name mangling 技术,将__membername替换成 _classname__membername,所以你在外部使用原来的私有成员的名字时,会提示找不到。”
“无论是单下划线还是双下划线开头的成员,都是希望外部程序开发者不要直接使用这些成员变量和这些成员函数,只是双下划线从语法上能够更直接的避免错误的使用,但是如果按照 _类名__成员名 则依然可以访问到。单下划线的在动态调试时可能会方便一些,只要项目组的人都遵守下划线开头的成员不直接使用,那使用单下划线或许会更好。”
1. Comparisons to singletons like None should always be done with is or is not, never the equality operators.
Also, beware of writing if x when you really mean if x is not None -- e.g. when testing whether a variable or argument that defaults to None was set to some other value. The other value might have a type (such as a container) that could be false in a boolean context!
2. When catching exceptions, mention specific exceptions whenever possible instead of using a bare except: clause.
For example, use:
try:
import platform_specific_module
except ImportError:
platform_specific_module = None
A bare except: clause will catch SystemExit and KeyboardInterrupt exceptions, making it harder to interrupt a program with Control-C, and can disguise other problems. If you want to catch all exceptions that signal program errors, use except Exception: (bare except is equivalent to except BaseException:).
3. For sequences, (strings, lists, tuples), use the fact that empty sequences are false.
Yes: if not seq:
if seq:
No: if len(seq)
if not len(seq)
4. Context managers should be invoked through separate functions or methods whenever they do something other than acquire and release resources. For example:
Yes:
with conn.begin_transaction():
do_stuff_in_transaction(conn)
No:
with conn:
do_stuff_in_transaction(conn)
The latter example doesn't provide any information to indicate that the __enter__ and __exit__ methods are doing something other than closing the connection after a transaction. Being explicit is important in this case.
5. Don't compare boolean values to True or False using ==.
Yes: if greeting:
No: if greeting == True:
Worse: if greeting is True:
摘自:
http://luochunfeng163.blog.163.com/blog/static/167009249201362453358567/
http://www.cnblogs.com/ToDoToTry/archive/2012/11/27/python_naming_conventions.html
http://www.educity.cn/wenda/354157.html
python的编码规范【摘】的更多相关文章
- Python代码编码规范
目录 1. Introduction 介绍 2. A Foolish Consistency is the Hobgoblin of Little Minds 尽信书,则不如无书 3. Code la ...
- 【翻译】Python PEP8编码规范(中文版)
原文链接:http://legacy.python.org/dev/peps/pep-0008/ item detail PEP 8 Title Style Guide for Python Code ...
- Python的编码规范(PEP 8 & Google Python guide)
PEP 8 Python 代码规范整理 click here Goole Python 风格指南 中文版 click here 大家有取舍的看吧. 因为文章不是原创的,所以只贴地址,给大家造成麻烦了, ...
- 【安全开发】python安全编码规范
申明:本文非笔者原创,原文转载自:https://github.com/SecurityPaper/SecurityPaper-web/blob/master/_posts/2.SDL%E8%A7%8 ...
- 【python】编码规范(转载)
转自:http://www.cnblogs.com/itech/archive/2012/01/06/2314454.html 1 编码 >>所有的 Python 脚本文件都应在文件头标上 ...
- [python]pep8编码规范
一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在操作符的后边敲回车.3 类 ...
- Python PEP8 编码规范中文版
原文链接:https://legacy.python.org/dev/peps/pep-0008/ 参考:https://blog.csdn.net/ratsniper/article/details ...
- Python 常用编码规范
一.简明概述 1.编码 如无特殊情况, 文件一律使用 UTF-8 编码 如无特殊情况, 文件头部必须加入#-*-coding:utf-8-*-标识 2.代码格式 2.1.缩进 统一使用 4 个空格进行 ...
- Python PEP8 编码规范
代码编排 缩进.缩进4个空格,不能混合使用Tab和空格. 每行最大长度79,文档字符串和注释行最大长度为72,换行可以使用反斜杠,最好使用圆括号. 类和顶层函数定义之间空两行:类中的方法定义以单行分隔 ...
随机推荐
- C和指针 第四章 习题
4.1正数的n的平方根可以通过: ai+1= (ai + n / ai ) / 2 得到,第一个a1是1,结果会越来越精确. #include <stdio.h> int main() { ...
- Effective C++ 33 避免遮掩继承而来的名称
首先介绍一个原则LSP(Liskov Substitution Principle),如果Class D以Public方式继承Class B,则所有B对象可以派上用场的任何地方,D对象一样可以派上用场 ...
- VIM下的跳转练习
在vim下可以使用常用的箭头键 但是 还有其它键可以让你更快的达到目标 hjkl 这是代替箭头键功能的 H M L 跳到屏幕的顶上 中间 下方 w 跳到下一个单词的开始e 跳到单词的结束b 向后跳 g ...
- WPF三大模板简介(Z)
WPF三大模板简介 WPF支持以下类型的模板: (1) 控件模板.控件模板可以将自定义模板应用到某一特定类型的所有控件,或是控件的某一实例.决定控件外观的是ControlTemplate,它决定了 ...
- VB中复制-粘贴-剪切键实现
If Me.ActiveControl.GetType.BaseType.ToString = "System.Windows.Forms.TextBoxBase" Then Wi ...
- Spring Schedule 任务调度实现
我们都知道任务调度可以用Quartz,但对于简单的定时任务,可以直接用Spring内置的Schedule来实现.可以由两种方式,注释+XML配置 注解方式: 注解也要先在sping.xml配置文件中配 ...
- 页面位置 top、postop、scrolltop、offsetTop、scrollHeight、offsetHeight、clientHe
1.top 此属性仅仅在对象的定位(position)属性被设置时可用.否则,此属性设置会被忽略. 代码如下: <div style=" position:absolute; widt ...
- iOS创建自定义的xib视图,不带控制器调用
1 我们平常使用视图都是在控制器中加载各种视图,但是有时候一个单独的视图,弄一个控制器未免有些显得太沉重了,所以我们现在来创建一个带xib加载视图的自定义视图 2 创建一个视图类集成uiview 3 ...
- SpringMVC客户端发送json数据时报400错误
当测试客户端发送json数据给服务器时,找不到响应路径? 原来是参数类型不符,即使是json也要考虑参数的个数和类型 解决:将age请求参数由"udf"改为"3" ...
- 如何解决自动加载与模板中(如Smarty)的自动加载冲突的问题
function aotuman($class){ include('./'.$class.'.class.php'); } spl_autoload_register('automan'); / ...