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
转载于:https://www.cnblogs.com/linyx/p/4716140.html
python的编码规范【摘】相关推荐
- Python PEP8 编码规范中文版
Python PEP8 编码规范中文版 2018年01月02日 19:21:09 阅读数:22140 标签: python 更多 个人分类: Python 原文链接:http://legacy.pyt ...
- [转载] Python pep8编码规范
参考链接: PEP 8:Python中的编码样式指南 原文链接:http://legacy.python.org/dev/peps/pep-0008/ itemdetailPEP8TitleStyle ...
- 【Python从入门到精通】(三)Python的编码规范,标识符知多少?
您好,我是码农飞哥,感谢您阅读本文,欢迎一键三连哦. 这是Pyhon系列文章的第三篇,本文主要介绍Python程序的编码规范. 干货满满,建议收藏,需要用到时常看看. 小伙伴们如有问题及需要,欢迎踊跃 ...
- Python PEP8编码规范
译 Python PEP8 编码规范中文版 2018年01月02日 19:21:09 冒冒大虾 阅读数:57340 原文链接:http://legacy.python.org/dev/peps/pep ...
- Python学习笔记 Python概述 编码规范 输出与输入 变量 标识符
Python学习第一天 Python的概述 1.Python的优缺点 1.1 优点: 1.2 缺点: 2.Python的编码规范 3.注释 3.Python的输出与输入 4.Python中的变量 5. ...
- Python的编码规范
Python的编码规范 一.前言 二.应该严格遵守的条目 一.前言 Python中采用PEP 8 作为编码规范,其中PEP是Python Enhancement Proposal的缩写,翻译过来是 ...
- python基础编码规范_Python语言的基本语法和编码规范.doc
Python 语言的基本语法和编码规范 Python 编程教程教师 : 工作 :Python 语言的基本语法和编码标 准课程描述本章将介绍 Python 语言的基本语法和编码标准,重点介 绍 Pyth ...
- Python的编码规范(超详细)
目录 一.前言 二.编写规范 三.命名规范 四.结语 一.前言 编码的规范性对代码的整体展现有着较大的影响. 先让我们看两张规范与不规范的代码截图来感受下. 先让我们看看不规范的吧. 看完有什么感觉吗 ...
- [Python]PEP8 编码规范及开发中的一些惯例和建议
为什么要有编码规范 编码是给人看的还是给机器看的? 美观是重点吗? 美观 可读性 可维护性 健壮性 团队内最好的代码状态: 所有人写出的代码像一个人写出来的 代码编排: 缩进 4 个空格, 禁止空格与 ...
最新文章
- android端使用http2.0,android Retrofit2+okHttp3使用总结
- 【R语言学习】时间序列
- 曙光服务器2008系统,在中科曙光I620-G20服務器上安裝Windows 2008 R2 系統步驟
- 剑指offer 旋转数组的最小数字
- vue2使用$set()使对象新增属性后触发视图更新
- Hbase的shell出现wrong number of arguments xxx以及undefined method any?for xxxx
- 使用 ABAP 代码解析一个 class 的所有方法
- react学习(28)---react挂载图
- Ubuntu安装pycharm并且激活
- BUAA-OO 第二单元作业“电梯调度”总结与思考
- 数据结构基础(8) --单链表的设计与实现(1)之基本操作
- 教老年人计算机心得体会,老年人教育工作心得体会
- Android对话框的高级设置《一》设置对话框按钮的图像和在内容文本中插入图像
- 解决matplotlib不显示图片
- 英特尔发布第三代全新可扩展处理器,加速5G网络转型
- Singer混沌映射(含MATLAB代码)
- excel服务器系统怎么登录,Excel Server Tutorial
- python fsolve说明_Python scipy fsolve“’func’参数的输入和输出...
- 达内python培训费_达内python的费用是多少?学习时间长吗?
- 【01 DualCam Porting】
热门文章
- 亲密关系-【舒适退出】-减少伤害的终局沟通
- 亲密关系沟通-【情感勒索】建立良性沟通
- docker创建镜像之Dockerfile
- css span 右端对齐_使用 CSS 实现具有方面感知的幽灵按钮
- python是最好的语言_Python转Crystal语言或许是个不错的选择
- oracle 安装的提示ntp,oracle rac 安装 PRVG-13606 ntp 同步报错解决过程
- java dayofweek_Java日期时间API系列22-----Jdk8中java.time包中的新的日期时间API类,Month月份和DayOfWeek星期的计算。...
- python匿名函数的使用介绍
- thinkphp mysql存储过程_MySql存储过程的创建与使用及在thinkphp中如何调用笔记
- 画虚线_夏天穿马丁靴?热死你!她仅在脚上画五条“虚线”,显高7cm