模块名:
小写字母,单词之间用_分割
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的编码规范【摘】的更多相关文章

  1. Python代码编码规范

    目录 1. Introduction 介绍 2. A Foolish Consistency is the Hobgoblin of Little Minds 尽信书,则不如无书 3. Code la ...

  2. 【翻译】Python PEP8编码规范(中文版)

    原文链接:http://legacy.python.org/dev/peps/pep-0008/ item detail PEP 8 Title Style Guide for Python Code ...

  3. Python的编码规范(PEP 8 & Google Python guide)

    PEP 8 Python 代码规范整理 click here Goole Python 风格指南 中文版 click here 大家有取舍的看吧. 因为文章不是原创的,所以只贴地址,给大家造成麻烦了, ...

  4. 【安全开发】python安全编码规范

    申明:本文非笔者原创,原文转载自:https://github.com/SecurityPaper/SecurityPaper-web/blob/master/_posts/2.SDL%E8%A7%8 ...

  5. 【python】编码规范(转载)

    转自:http://www.cnblogs.com/itech/archive/2012/01/06/2314454.html 1 编码 >>所有的 Python 脚本文件都应在文件头标上 ...

  6. [python]pep8编码规范

    一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在操作符的后边敲回车.3 类 ...

  7. Python PEP8 编码规范中文版

    原文链接:https://legacy.python.org/dev/peps/pep-0008/ 参考:https://blog.csdn.net/ratsniper/article/details ...

  8. Python 常用编码规范

    一.简明概述 1.编码 如无特殊情况, 文件一律使用 UTF-8 编码 如无特殊情况, 文件头部必须加入#-*-coding:utf-8-*-标识 2.代码格式 2.1.缩进 统一使用 4 个空格进行 ...

  9. Python PEP8 编码规范

    代码编排 缩进.缩进4个空格,不能混合使用Tab和空格. 每行最大长度79,文档字符串和注释行最大长度为72,换行可以使用反斜杠,最好使用圆括号. 类和顶层函数定义之间空两行:类中的方法定义以单行分隔 ...

随机推荐

  1. Virtual Box下配置Host-Only联网方式详解

    其实网络这类相关的文章很多,我只是想结合自己的实际情况,把我的经验写下来,给那些需要的人们吧. 主机:windows 7 虚拟机:CentOS6.0 VirtualBox:4.2.0 虚拟机在安装好之 ...

  2. 在浏览器上直接输入url 时,中文传参乱码问题

    这样的地址 xxx.asp?name=中国  ,通过 超链接打开这个链接 ,xxx.asp能够成才接收参数,但是如果将地址直接放到浏览器地址栏上,回车, xxx.asp就无法正确接收中文参数,一直显示 ...

  3. c语言中的一些注意点

    1.头文件两种形式的区别(#include<mystring.h>与#include"mystring.h") 当运行一个程序时,需要调用自己写的函数时,需要在头文件加 ...

  4. SQL/T-SQL实例参考

    ,D.[Score] B_Score ,'Distince'= CASE WHEN C.Score > D.Score THEN C.[Score] - D.[Score] WHEN C.Sco ...

  5. Unity3D 查找Update函数体为空的类

    如果是大项目,有很多Update空跑还是多少有些效率损耗,那我们就把他们都找出来. 先引用Mono.Cecil //代码 using UnityEngine; using UnityEditor; u ...

  6. js 中 substring() 和 substr() 提取字符

    提取字符串substring() substring() 方法用于提取字符串中介于两个指定下标之间的字符. 语法: stringObject.substring(startPos,stopPos)  ...

  7. 改写js原装的alert样式

    1.将下面的js代码单独到一个js文件中,然后在页面中引用 AlertDialog.js //改写js原装的alert样式 var t; var timeclose = 0; var showBack ...

  8. jquery ashx交互 返回list 循环json输入信息

    html代码:触发按钮 <input type="button" id="search" value="查询" /> ashx代 ...

  9. Nginx 反代理其他搜索引擎

    反向代理 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客 ...

  10. Power BI for Office 365(七) Power BI站点

    报表分享是Power BI for Office 365比较关键的一个功能,其中提供的Power BI站点将给这些报表一个安全的"归宿",同时也可以跟任何想要分享的人进行协作--- ...