调用具有长参数列表的函数时的最后括号

时间:2016-10-24 09:00:04

标签: python pep8

当使用长参数列表调用函数时,右括号是否应该在单独的行上?例如:

import module1.module2

def main():

    # alternative 1, closing parenthesis on separate line
    x=3
    y=4
    my_result_name_1, my_result_name_2 = module1.module2.function3(
        argument_name1, argument_name2, keyword_argument=(x,y)
    )
    print(my_result_name_1)

    # alternative 2, closing parenthesis on same line as last argument
    x=3
    y=4
    my_result_name_1, my_result_name_2 = module1.module2.function3(
        argument_name1, argument_name2, keyword_argument=(x,y))
    print(my_result_name_1)

    return

if __name__ == '__main__':
    main()

PEP8中的示例对我来说有点混乱。首先他们有这个例子:

# Hanging indents should add a level.
foo = long_function_name(
    var_one, var_two,
    var_three, var_four)

然后,他们有了这个:

my_list = [
    1, 2, 3,
    4, 5, 6,
]

首选哪种风格?

1 个答案:

答案 0 :(得分:1)

就个人而言,当我只需要第二行时;我将开括号和近括号放在一行,例如:

foo = long_function_name(var_one, var_two,
                         var_three, var_for)

但是,如果我要增加一条额外的行,我会将内容分开,例如:

my_list = [
    1, 2, 3,
    4, 5, 6,
    7, 8, 9
]

真的由你来决定你想怎么做。对我来说,最重要的是可读性和一致性。