python 规范
摘自google.
https://i.cnblogs.com/PostDone.aspx?postid=9753605&actiontip=%E4%BF%9D%E5%AD%98%E4%BF%AE%E6%94%B9%E6%88%90%E5%8A%9F
1、缩进
Tip
用4个空格来缩进代码
2、代码太长:
如果一个文本字符串在一行放不下, 可以使用圆括号来实现隐式行连接:
x = ('This will build a very long long '
'long long long long long long string')
或者:
print \\
"hello world!"
3、类:
class SampleClass(object):
4、注释:
"""Explicitly inherits from another class already.""" 或者 #Explicitly inherits from another class already.
5、字符串:
通常可以用+,%,join(), format 三个函数进行处理
%s #字符串
%d #数字
Yes: x = a + b
x = '%s, %s!' % (imperative, expletive)
x = '{}, {}!'.format(imperative, expletive)
x = 'name: %s; score: %d' % (name, n)
x = 'name: {}; score: {}'.format(name, n)
Join:
items = ['ni','hao'];
employee_table = ''.join(items);
6、处理文件:
1、with open("hello.txt") as hello_file:
for line in hello_file:
print line
2、import contextlib
with contextlib.closing(urllib.urlopen("http://www.python.org/")) as front_page:
for line in front_page:
print line
7、变量等命名规范:
Python之父Guido推荐的规范 Type Public Internal
Modules lower_with_under _lower_with_under
Packages 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
python 规范的更多相关文章
- PEP8 python规范神器
如需转载,请注明出处:小婷儿的博客:https://www.cnblogs.com/xxtalhr/p/10645992.html 一.Jupyter notebook 篇 Jupyter noteb ...
- Google实践中总结的Python规范,get了吗?
好的代码风格,给人舒服的感觉,今天介绍一下谷歌的Python风格规范 1 分号 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 2 行长度 每行不超过80个字符:不要使用反斜杠连接行.Pyth ...
- Python规范:提高可读性
PEP 8 规范 PEP 是 Python Enhancement Proposal 的缩写,翻译过来叫"Python 增强规范". 缩进规范 PEP 8 规范告诉我们,请选择四个 ...
- Python规范:代码规范要注意
主要有以下两种代码规范 <8 号 Python 增强规范>(Python Enhacement Proposal #8),以下简称 PEP8: <Google Python 风格规范 ...
- 基础python规范
一.注释 合理的代码注释应该占源代码的 1/3 左右,Python 语言允许在任何地方插入空字符或注释,但不能插入到标识符和字符串中间. 在 Python 中,通常包括 3 种类型的注 ...
- Python规范:用用assert
什么是assert assert的语法: assert_stmt ::= "assert" expression ["," expression] 例: ass ...
- python 规范篇 如何合理使用 assert
assert 的合理使用,可以增加代码的健壮度,同时也方便了程序出错时开发人员的定位排查. 什么是 assert? Python 的 assert 语句,可以说是一个 debug 的好工具,主要用于测 ...
- Python规范
1.命名 Django文件命名 小写+下划线 类:驼峰 2.edit 执行环境 work direction 到当前项目目录 3.类要加注释 4.去数据库找数据时需要try捕获异常,防止数据库连接断掉 ...
- python多态和规范
python规范(接口类) 接口类可以规范代码,但接口类本身是不实现的 class Payment: def pay(self,money): raise Notlmplemented class W ...
随机推荐
- 字符串截取函数slice, substring, substr
在日常项目需求中,常常会遇到需要截取字符串操作的工作,而ECMAScript为我们提供了原生的截取字符串的函数,而且提供了三个:slice, substring, substr.我们怎么判断在什么时候 ...
- 安全测试4_客户端的安全漏洞(XSS、CSRF、点击劫持、URL跳转)
那个fanh前面学习的都是基础,现在开始正式学习下安全的知识,这一章主要讲解客户端常见的安全漏洞. 看到这个不错,给大家记一下: 1.常见的安全事件: 2.XSS(跨站脚本),英文全称:Cross S ...
- Error building Player: UnityException: Bundle Identifier has not been set up correctly
错误提示: Error building Player: UnityException: Bundle Identifier has not been set up correctlyPlease s ...
- swt text 回车 defaultSelected
今天试了一下SWT控件 TEXT 中的回车事件,使用 defaultSelected 进行处理,结果怎么也不能触发事件. 经过仔细排查,发现是TEXT选中了 wrap 的原因,毕竟如果是多行的话,肯定 ...
- Docker的安装和启动
2.Docker安装与启动 2.1安装环境说明 Docker官方建议在Ubuntu中安装,因为Docker是基于Ubuntu发布的,而且一般Docker出现的问题Ubuntu是最先更新或者打补丁的.在 ...
- 27.纯 CSS 创作一个精彩的彩虹 loading 特效
原文地址:https://segmentfault.com/a/1190000014939781 感想:正方形->圆->旋转一定角度->动画->隐藏下一半 HTML代码: &l ...
- 8.纯 CSS 创作一个充电 loader 特效
原文地址:https://segmentfault.com/a/1190000014669547 右边多出来的是 :after 的border HTML代码: <div class=" ...
- 《算法》第六章部分程序 part 2
▶ 书中第六章部分程序,包括在加上自己补充的代码,B-树 ● B-树 package package01; import edu.princeton.cs.algs4.StdOut; public c ...
- Image.Url 无法使用 Server.MapPath(使用后无论如何也不显示)
Image.Url 无法使用 Server.MapPath(使用后无论如何也不显示)
- 03.windows系统重新分配ip的cmd命令
网络重启CMD命令 ipconfig /release --- 释放ip搜索 ipconfig /renew --- 重新获得