简体   繁体   English

函数调用的Python编码样式有多个参数

[英]Python coding style on function call with multiple parameters

This may sound like a newbie question, but I really need some help with this. 这可能听起来像一个新手问题,但我真的需要一些帮助。 I don't even know how to tag it, but I assume is just a Python question. 我甚至不知道如何标记它,但我认为只是一个Python问题。 I need to use a function that receives 5 parameters and returns 5 values: 我需要使用一个接收5个参数并返回5个值的函数:

a, b, c, d, e = function (input1, input2, input3, input4, input5)

The problem is that when I use the full variables/functions names the line is just too long, and the code looks ugly, I want to use a more fancy solution here and I was thinking on using a dict or a list so I can do this: 问题是,当我使用完整的变量/函数名称时,行太长了,代码看起来很难看,我想在这里使用一个更奇特的解决方案,我正在考虑使用字典或列表,所以我可以做这个:

input_dict['param1'] = input1
input_dict['param2'] = input2
input_dict['param3'] = input3
input_dict['param4'] = input4
input_dict['param5'] = input5
ret_dict = function(input_dict)

Is there a better or "pythonic" way of improving the code quality of this kind of calls? 有没有更好或“pythonic”的方式来提高这种电话的代码质量?

Thanks in advance! 提前致谢!

You can nest lines like this 你可以像这样嵌套线条

my_function(test, this, out, like, so,
                   something, indent)

You can expand lists into args like so 您可以将列表扩展为args,就像这样

a = [1,2,3,4,5]
my_function(*a)

You can even do this 你甚至可以这样做

result = my_function(big, long, list, 
                      of, args)
a,b,c,d = result

Also, PEP-8 has good recommendations on dealing with line length. 此外, PEP-8在处理线路长度方面有很好的建议。 – Lattyware 5 mins ago - Lattyware 5分钟前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM