标签:顺序 tps ash 显示 cstring max zmq index overflow
目前的规范基于pep-0008
使用4个空格进行缩进
每行代码尽量不超过80个字符
理由:
Python支持括号内的换行。这时有两种情况。
foo = long_function_name(var_one, var_two,
var_three, var_four)
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
使用反斜杠\
换行,二元运算符+
.
等应出现在行首,并与上一行的.
或=
对齐;或者缩进4个空格。长字符串也可以用此法换行
foo = variable_with_long_name + another_variable + variable
session.query(MyTable) .filter_by(id=1) .one()
this_is_a_very_long(function_call, ‘with many parameters‘) .that_returns_an_object_with_an_attribute
print ‘Hello, ‘ ‘%s %s!‘ % (‘Harry‘, ‘Potter‘)
多个元素的list或者tuple,在起始括号后换行,第二行缩进4个空格
items = [
‘this is the first‘, ‘set of items‘, ‘with more items‘,
‘to come in this line‘, ‘like this‘
]
禁止复合语句,即一行中包含多个语句:
# yes
do_first()
do_second()
do_third()
# no
do_first();do_second();do_third();
if/for/while
一定要换行:
# yes
if foo == ‘blah‘:
do_blah_thing()
# no
if foo == ‘blah‘: do_blash_thing()
class A:
"""This is a simple docstring."""
def __init__(self):
pass
def hello(self):
pass
def hello(name):
print "Hello %s!" % name
def main():
pass
[=,-,+=,==,>,in,is not, and]
:# yes
exp = -1.05
i = i + 1
submitted += 1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)
# no
exp = - 1.05
i=i+1
submitted +=1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)
,
之后要有空格# yes
def complex(real, imag):
pass
# no
def complex(real,imag):
pass
# yes
def complex(real, imag=0.0):
pass
# no
def complex(real, imag = 0.0):
pass
# yes
spam(ham[1], {eggs: 2})
value = my_list[index]
# no
spam( ham[1], { eggs : 2 } )
value = my_list[ index ]
# yes
dict[‘key‘] = list[index]
# no
dict [‘key‘] = list [index]
# yes
x = 1
y = 2
long_variable = 3
# no
x = 1
y = 2
long_variable = 3
True
、False
的比较foo not in bar
的形式,而不是not foo in bar
instance(a, C)
进行实例的类型检查,而不是type(A) is C
# yes
if method == ‘md5‘:
pass
if not foo:
pass
if foo not in bar:
pass
if instance(a, C):
pass
# no
if ‘md5‘ == method:
pass
if foo == False:
pass
if not foo in bar:
pass
if type(A) is C:
pass
简单说,自然语言使用双引号,机器标示使用单引号,因此 代码里 多数应该使用 单引号
"..."
u"你好世界"
‘...‘
例如dict里的keyr"..."
"""......"""
# yes
import os
import sys
# no
import sys,os
# yes
from subprocess import Popen, PIPE
# yes
from foo.bar import Bar
# no
from ..bar import Bar
import os
import sys
import msgpack
import zmq
import foo
from myclass import MyClass
import bar
import foo.bar
bar.Bar()
foo.bar.Bar()
#
号后空一格,段落间用空行分开(同样需要#
号)
# 块注释
# 块注释
#
# 块注释
# 块注释
至少使用两个空格和语句分开,使用有意义的注释
# yes
x = x + 1 # 边框加粗一个像素
# no
x = x + 1 # x加1
docstring的规范在 PEP 257 中有详细描述,其中最其本的两点:
"""Return a foobar
Optional plotz says to frobnicate the bizbaz first.
"""
"""Oneline docstring"""
l(L)
,大写字母O(o)
或I(i)
单独作为一个变量的名称,以区分数字1
和0
CamelCase
命名风格,内部类可用一个下划线开头;HTTPWriter
而不是HttpWriter
lowercase_with_underscores
UPPERCASE_WITH_UNDERSCORES
name_re
MAX_OVERFLOW = 100
Class FooBar:
def foo_bar(self, print_):
print(print_)
Function and method arguments:
cls
as first parameter.self
as first parameter.x
, as in display_name = property(lambda x: x.real_name or x.username)
.#-*-coding:utf-8-*-
标识标签:顺序 tps ash 显示 cstring max zmq index overflow
原文地址:http://www.cnblogs.com/JZ-Ser/p/7056332.html