请考虑以下版本的代码:
def __init__(self, broker, group, offset='largest', commit_every_x_ms=1000, parts=None): # ^ pass
VS
def __init__(self, broker, group, offset='largest', commit_every_t_ms=1000, parts=None): # ^ pass
我认为第二个选项看起来更好,因为它可以节省具有非常大的名称的函数所需的行,但哪一个将被认为更pythonic?
请参阅PEP 0008
(https://www.python.org/dev/peps/pep-0008/):
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)