0

I have a function that returns some items as a tuple. This function breaks a range of values into quintiles

Example:

def my_function1():

    #does some stuff

    return first_quintile_max, first_quintile_min, second_quintile_max, second_quintile_min, third_quintile_max, third_quintile_min, fourth_quintile_max, fourth_quintile_min, fifth_quintile_max, fifth_quintile_min

So now my output from this function is a tuple (range_of_values), with 10 items in it.

range_of_values = my_function1()

I've named these (not as namedtuples, but I suppose I could do that) and used them as arguments for the next function:

    first_quintile_max = range_of_values[0]
    first_quintile_min = range_of_values[1]
    second_quintile_max = range_of_values[2]
    second_quintile_min = range_of_values[3]
    third_quintile_max = range_of_values[4]
    third_quintile_min = range_of_values[5]
    fourth_quintile_max = range_of_values[6]
    fourth_quintile_min = range_of_values[7]
    fifth_quintile_max = range_of_values[8]
    fifth_quintile_min = range_of_values[9]

Now I run the next function, using these as my arguments:

my_function2(first_quintile_max, first_quintile_min, second_quintile_max, second_quintile_min,
                      third_quintile_max, third_quintile_min, fourth_quintile_max, fourth_quintile_min, 
                      fifth_quintile_max, fifth_quintile_min)

I can't help but think there is a more elegant way to feed this tuple into my second function as arguments. Can anyone offer a suggestion?

Erich Purpur
  • 1,337
  • 3
  • 14
  • 34

2 Answers2

1

Just use my_function2(* range_of_values). You'll have to assure that range_of_values is sorted the same way the my_function2 parameters do.

ivallesp
  • 2,018
  • 1
  • 14
  • 21
0

You're right, it's not too great to pass so many values as a parameter into a function. It's better instead to pack them into a data structure, like a dictionary:

quintiles = {
    'first': (max, min),
    'second': (max, min),
    # ...
}

Then return this from your function. Then you just pass the dictionary to your other function and access the elements like this:

quintiles['first']

Hope that helps!

Andrew Grass
  • 242
  • 1
  • 8