gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:02:26 26 4
gpt4 key购买 nike

当调用带有长参数列表的函数时,右括号应该在单独的一行上吗?例如:

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,
]

首选哪种风格?

最佳答案

就我个人而言,当我只需要第二条附加线时;我将左括号和右括号放在一起,例如:

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

但是,如果我要添加不止一行,我会将内容保留在单独的行上,例如:

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

实际上,由您决定如何去做。对我来说,最重要的是可读性和一致性。

关于python - 调用具有长参数列表的函数时的最后括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40214405/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com