码迷,mamicode.com
首页 > 编程语言 > 详细

Python PEP8 编码规范 表达式和语句中的空格

时间:2018-07-22 21:39:47      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:final   offset   argument   variable   类型   set   函数参数   不同   eal   

不能忍受的事情

在下列情况下,避免使用无关的空格:

  • 紧跟在小括号,中括号或者大括号后。
Yes: spam(ham[1], {eggs: 2})
No:  spam( ham[ 1 ], { eggs: 2 } )
  • 紧贴在逗号、分号或者冒号之前。
Yes: if x == 4: print x, y; x, y = y, x
No:  if x == 4 : print x , y ; x , y = y , x
  • 然而,冒号在切片中就像二元运算符,在两边应该有相同数量的空格(把它当做优先级最低的操作符)。在扩展的切片操作中,所有的冒号必须有相同的间距。例外情况:当一个切片参数被省略时,空格就被省略了。 
    推荐:

 

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]

  不推荐

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]
  • 为了和另一个赋值语句对齐,在赋值运算符附件加多个空格。 
    推荐:
x = 1
y = 2
long_variable = 3

不推荐:

x             = 1
y             = 2
long_variable = 3

 

其他建议

  • 避免在尾部添加空格。因为尾部的空格通常都看不见,会产生混乱:比如,一个反斜杠后面跟一个空格的换行符,不算续行标记。有些编辑器不会保留尾空格,并且很多项目(像CPython)在pre-commit的挂钩调用中会过滤掉尾空格。
  • 总是在二元运算符两边加一个空格:赋值(=),增量赋值(+=,-=),比较(==,<,>,!=,<>,<=,>=,in,not,in,is,is not),布尔(and, or, not)。
  • 如果使用具有不同优先级的运算符,请考虑在具有最低优先级的运算符周围添加空格。有时需要通过自己来判断;但是,不要使用一个以上的空格,并且在二元运算符的两边使用相同数量的空格。 
    推荐:
i = i + 1
submitted += 1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)

不推荐:

i=i+1
submitted +=1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)
  • 在制定关键字参数或者默认参数值的时候,不要在=附近加上空格。 
    推荐:
def complex(real, imag=0.0):
    return magic(r=real, i=imag)

不推荐:

def complex(real, imag = 0.0):
    return magic(r = real, i = imag)
  • 功能型注释应该使用冒号的一般性规则,并且在使用->的时候要在两边加空格。(参考下面的功能注释得到能够多信息) 
    推荐:
def munge(input: AnyStr): ...
def munge() -> AnyStr: ...

不推荐:

def munge(input:AnyStr): ...
def munge()->PosInt: ...
  • 当给有类型备注的参数赋值的时候,在=两边添加空格(仅针对那种有类型备注和默认值的参数)。 
    推荐:
def munge(sep: AnyStr = None): ...
def munge(input: AnyStr, sep: AnyStr = None, limit=1000): ...

不推荐:

def munge(input: AnyStr=None): ...
def munge(input: AnyStr, limit = 1000): ...
  • 复合语句(同一行中的多个语句)通常是不允许的。 
    推荐:
if foo == ‘blah‘:
    do_blah_thing()
do_one()
do_two()
do_three()

不推荐:

if foo == ‘blah‘: do_blah_thing()
do_one(); do_two(); do_three()
  • 虽然有时候将小的代码块和 if/for/while 放在同一行没什么问题,多行语句块的情况不要这样用,同样也要避免代码行太长! 
    最好别这样:
if foo == ‘blah‘: do_blah_thing()
for x in lst: total += x
while t < 10: t = delay()

不推荐:

if foo == ‘blah‘: do_blah_thing()
else: do_non_blah_thing()

try: something()
finally: cleanup()

do_one(); do_two(); do_three(long, argument,
                             list, like, this)

if foo == ‘blah‘: one(); two(); three()

 

Python PEP8 编码规范 表达式和语句中的空格

标签:final   offset   argument   variable   类型   set   函数参数   不同   eal   

原文地址:https://www.cnblogs.com/40kuai/p/9351506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!