python pep8_python PEP8相关介绍

#括号紧跟的前后

Yes: spam(ham[1], {eggs: 2})

No: spam( ham[ 1 ], { eggs: 2 } )

#逗号加反括号

Yes: foo = (0,)

No: bar = (0, )

#逗号,分号,冒号前

Yes: if x == 4: print x, y; x, y = y, x

No: if x == 4 : print x , y ; x , y = y , x

#切片中的特殊情况:如果有表达式,应保证冒号左右对称(数字不加空格)

Yes:

ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]

ham[lower:upper], ham[lower:upper:], ham[lower::step]

ham[lower+offset : upper+offset]

ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]

ham[lower + offset : upper + offset]

No:

ham[lower + offset:upper + offset]

ham[1: 9], ham[1 :9], ham[1:9 :3]

ham[lower : : upper]

ham[ : upper]

#函数或变量的左括号,索引或切片均不加空格

Yes: spam(1)

No: spam (1)

Yes: dct['key'] = lst[index]

No: dct ['key'] = lst [index]

#等号两边各加一个空格,不可为了对其而多加

Yes:

x = 1

y = 2

long_variable = 3

No:

x = 1

y = 2

long_variable = 3

#这些运算符左右各加一个空格:assignment (=), augmented assignment (+=, -= etc.), comparisons (==, , !=, <>, <=, >=, in, not in, is, is not), Booleans (and, or, not).保证左右对称

Yes:

i = i + 1

submitted += 1

x = x*2 - 1

hypot2 = x*x + y*y

c = (a+b) * (a-b)

No:

i=i+1

submitted +=1

x = x * 2 - 1

hypot2 = x * x + y * y

c = (a + b) * (a - b)

#关键字或默认赋值时的等号不添加空格:

Yes:

def complex(real, imag=0.0):

return magic(r=real, i=imag)

No:

def complex(real, imag = 0.0):

return magic(r = real, i = imag)

#函数的注释冒号紧跟变量名,空一格后写注释,返回值注释->前后均空一格,当给有注释的赋默认值时应在等于前后加空格

Yes:

def munge(input: AnyStr): ...

def munge() -> AnyStr: ...

No:

def munge(input:AnyStr = None): ...

def munge()->PosInt: ...

#混合声明一般不被允许,while,if除外

Yes:

if foo == 'blah':

do_blah_thing()

do_one()

do_two()

do_three()

Rather not:

if foo == 'blah': do_blah_thing()

do_one(); do_two(); do_three()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值