两种 rop 保留 ash 缩进 and 可能 diff absolut
使用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
简单说,自然语言使用双引号,机器标示使用单引号,因此 代码里 多数应该使用 单引号
例如错误信息;很多情况还是unicode,使用
- "..."
- u"你好世界"
例如dict里的key
- ‘...‘
- r"..."
- """......"""
- # 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:
as first parameter.
- cls
as first parameter.
- self
, as in
- x
.
- display_name = property(lambda x: x.real_name or x.username)
标识
- #-*-coding:utf-8-*-
团队编程项目代码设计规范(爬取豆瓣电影top250)
来源: http://www.bubuko.com/infodetail-2331416.html