Python 将方法签名继续到新行的约定
考虑以下版本的代码:Python 将方法签名继续到新行的约定,python,Python,考虑以下版本的代码: def __init__(self, broker, group, offset='largest', commit_every_x_ms=1000, parts=None): # ^ pass vs 我认为第二个选项看起来更好,因为它可以保留具有非常大名称的函数所需的行,但哪一个会被认为更具pythonic?请参阅PEP 0008(): Python有一个(PEP-0008),它对类似的事情有建议。如上所述: 确保连
def __init__(self, broker, group, offset='largest', commit_every_x_ms=1000,
parts=None):
# ^
pass
vs
我认为第二个选项看起来更好,因为它可以保留具有非常大名称的函数所需的行,但哪一个会被认为更具pythonic?请参阅
PEP 0008
():
Python有一个(PEP-0008),它对类似的事情有建议。如上所述:
确保连续行适当缩进。绕过二进制运算符的首选位置是运算符之后,而不是运算符之前。一些例子:
class Rectangle(Blob):
def __init__(self, width, height,
color='black', emphasis=None, highlight=0):
if (width == 0 and height == 0 and
color == 'red' and emphasis == 'strong' or
highlight > 100):
raise ValueError("sorry, you lose")
if width == 0 and height == 0 and (color == 'red' or
emphasis is None):
raise ValueError("I don't think so -- values are %s, %s" %
(width, height))
Blob.__init__(self, width, height,
color, emphasis, highlight)
还可以查看该部分以获取更多示例
Yes:
# Aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
var_three, var_four)
# More indentation included to distinguish this from the rest.
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
# Hanging indents should add a level.
foo = long_function_name(
var_one, var_two,
var_three, var_four)
No:
# Arguments on first line forbidden when not using vertical alignment.
foo = long_function_name(var_one, var_two,
var_three, var_four)
# Further indentation required as indentation is not distinguishable.
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
class Rectangle(Blob):
def __init__(self, width, height,
color='black', emphasis=None, highlight=0):
if (width == 0 and height == 0 and
color == 'red' and emphasis == 'strong' or
highlight > 100):
raise ValueError("sorry, you lose")
if width == 0 and height == 0 and (color == 'red' or
emphasis is None):
raise ValueError("I don't think so -- values are %s, %s" %
(width, height))
Blob.__init__(self, width, height,
color, emphasis, highlight)