模块级别的“双下划线名称”(即带有两个前导和两个尾随下划线的名称),如__all__、__author__、__version__等,应该放在模块文档字符串之后,但在除__future__导入之外的任何导入语句之前。Python规定,未来导入(future-imports)必须出现在模块中的任何代码之前,但文档字符串除外:
(在Python中,遵循PEP 8风格指南时,模块的布局很重要。这包括在模块中定义各种元素(如变量、函数、类等)的顺序。对于模块级别的特殊变量,如__all__(用于指定当使用from module import *时应该导入哪些名称)、__author__(用于指定模块的作者)、__version__(用于指定模块的版本号)等,PEP 8建议将它们放置在模块文档字符串之后。
此外,PEP 8还指出,这些双下划线名称(通常称为“dunders”)应该出现在除__future__导入之外的任何导入语句之前。__future__导入是Python中的一种特殊导入,它允许你使用未来版本的Python中引入的新特性,即使你当前使用的是旧版本的Python。由于这些导入可能会改变Python的语法或行为,因此它们必须放在模块中的任何代码之前(除了文档字符串)。
因此,当你编写Python模块时,你应该首先放置文档字符串,然后是__future__导入(如果有的话),接着是模块级别的双下划线名称(如__all__、__author__、__version__等),最后是其他的导入语句和模块代码。这样的布局有助于保持代码的组织性和可读性。)
"""This is the example module. This module does stuff. """ from __future__ import barry_as_FLUFL __all__ = ['a', 'b', 'c'] __version__ = '0.1' __author__ = 'Cardinal Biggles' import os import sys
在Python中,单引号字符串和双引号字符串是等价的。本PEP(Python增强提案)并未对此做出推荐。你应该选择一种规则并坚持使用。然而,当字符串中包含单引号或双引号字符时,应使用另一种引号以避免在字符串中使用反斜杠。这样做可以提高代码的可读性。
对于三引号字符串,请始终使用双引号字符,以保持与PEP 257中文档字符串约定的一致性。
避免在以下情况下出现多余空格:
# 正确的: spam(ham[1], {eggs: 2}) # 错误的: spam( ham[ 1 ], { eggs: 2 } )
# 正确的: foo = (0,) # 错误的: bar = (0, )
# 正确的: if x == 4: print(x, y); x, y = y, x # 错误的: 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 : : step] ham[ : upper]
# 正确的: spam(1) # 错误的: spam (1)
# 正确的: dct['key'] = lst[index] # 错误的: dct ['key'] = lst [index]
# 正确的: x = 1 y = 2 long_variable = 3 # 错误的: x = 1 y = 2 long_variable = 3
# 正确的: 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 munge(input: AnyStr): ... def munge() -> PosInt: ... # 错误的: def munge(input:AnyStr): ... def munge()->PosInt: ...
当等号(=)用于指示关键字参数,或者用于指示未加注解的函数参数的默认值时,不要在等号周围使用空格。
# 正确的: 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(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 += xwhile 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()