Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python coding style on function call with multiple parameters

Tags:

python

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. I need to use a function that receives 5 parameters and returns 5 values:

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?

Thanks in advance!

like image 816
Sergio Ayestarán Avatar asked Jan 02 '13 17:01

Sergio Ayestarán


1 Answers

You can nest lines like this

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

You can expand lists into args like so

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. – Lattyware 5 mins ago

like image 194
Jakob Bowyer Avatar answered Oct 17 '22 05:10

Jakob Bowyer