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

时间: 2024-08-24 16:26:35

python的编码规范【摘】的相关文章

【python】编码规范(转载)

转自:http://www.cnblogs.com/itech/archive/2012/01/06/2314454.html 1 编码 >>所有的 Python 脚本文件都应在文件头标上如下标识或其兼容格式的标识: # -*- coding:utf-8 -*- >>设置编辑器,默认新建或保存为utf-8格式. 2 注释 >>业界普遍认同 Python 的注释分为两种的概念,一种是由 # 开头的"真正的"注释,另一种是 docstrings.前者表明

[python]pep8编码规范

一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在操作符的后边敲回车.3 类和top-level函数定义之间空两行:类中的方法定义之间空一行:函数内逻辑无关段落之间空一行:其他地方尽量不要再空行. 二 文档编排1 模块内容的顺序:模块说明和docstring—import—globals&constants—其他定义.其中import部分,又按标准.三方和自己编写顺序依次排

(转)Python PEP8 编码规范中文版

转:https://blog.csdn.net/ratsniper/article/details/78954852 原文链接:http://legacy.python.org/dev/peps/pep-0008/ item detail PEP 8 Title Style Guide for Python Code Version c451868df657 Last-Modified 2016-06-08 10:43:53 -0400 (Wed, 08 Jun 2016) Author Gui

Python PEP8 编码规范中文版-译自官网文件

写在前面(自补):初听PEP8一头雾水,不知所谓.啥是PEP8?为啥叫PEP8?PEP8是干啥的?-先了解下PEP吧. PEP是什么? PEP的全称是Python Enhancement Proposals,其中Enhancement是增强改进的意思,Proposals则可译为提案或建议书,所以合起来,比较常见的翻译是Python增强提案或Python改进建议书. 我个人倾向于前一个翻译,因为它更贴切.Python核心开发者主要通过邮件列表讨论问题.提议.计划等,PEP通常是汇总了多方信息,经过

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

PEP 8 Python 代码规范整理 click here Goole Python 风格指南 中文版 click here 大家有取舍的看吧. 因为文章不是原创的,所以只贴地址,给大家造成麻烦了,见谅. 网络不好,就没有贴Python官方的地址,大家可以自己找找看.我在离线文档里面没有找到,点击PEP 8不会跳转,在文档内搜索PEP 8也没有找到,知道原因的麻烦告知一下. 作为一个程序员有必要去了解和遵守编码规范,给大家带来方便的同时也方便了自己.编程也是讲究艺术的. 欢迎关注我的微博交流

Python pep8 编码规范

pep8规范 官方文档:https://www.python.org/dev/peps/pep-0008/ PEP8中文翻译:http://www.cnblogs.com/ajianbeyourself/p/4377933.html 以下内容转自 https://www.douban.com/note/134971609/ PEP8 Python 编码规范 一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用

python/django编码规范(code style)

转自:http://pythoner.org/wiki/10/ 编码 所有的 Python 脚本文件都应在文件头标上 # -*- coding:utf-8 -*- .设置编辑器,默认保存为 utf-8 格式.注释    业界普遍认同 Python 的注释分为两种的概念,一种是由 # 开头的"真正的"注释,另一种是 docstrings.前者表明为何选择当前实现以及这种实现的原理和难点,后者表明如何使用这个包.模块.类.函数(方法),甚至包括使用示例和单元测试.    坚持适当注释原则.

Python PEP8 编码规范中文版

原文链接:https://legacy.python.org/dev/peps/pep-0008/ 参考:https://blog.csdn.net/ratsniper/article/details/78954852 Code lay-out 代码布局 Indentation 缩进    Tabs or Spaces 制表符还是空格    Maximum Line Length 行的最大长度    Should a line break before or after a binary ope

Python 常用编码规范

一.简明概述 1.编码 如无特殊情况, 文件一律使用 UTF-8 编码 如无特殊情况, 文件头部必须加入#-*-coding:utf-8-*-标识 2.代码格式 2.1.缩进 统一使用 4 个空格进行缩进 2.2.行宽 每行代码尽量不超过 80 个字符(在特殊情况下可以略微超过 80 ,但最长不得超过 120) 理由: 这在查看 side-by-side 的 diff 时很有帮助 方便在控制台下查看代码 太长可能是设计有缺陷 2.3.引号 简单说,自然语言使用双引号,机器标示使用单引号,因此 代