7

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!

4
  • I can't say I entirely understand what it is exactly that you find ugly about the original code, and in what way the input_dict version is prettier. Commented Jan 2, 2013 at 17:14
  • Note that there is a dict literal notation in Python so rather than many assignments, you should construct dictionaries as {x: y, w: z}. Commented Jan 2, 2013 at 17:16
  • 3
    Also, PEP-8 has good recommendations on dealing with line length. Commented Jan 2, 2013 at 17:17
  • Thanks Lattyware, this helps me too! Commented Jan 3, 2013 at 21:25

1 Answer 1

11

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.