0

I have a python method of a class which is calculating a bunch of stuff, stores them in 8 different variables and then want to return these values.

Something on the lines;

def rate_lookup(self, a):
   ....
   ....

   return(charge, 
              handling_charge,
              delivery_charge,
              fuel_surcharge,
              overheight_surcharge,
              security_charge,
              documentation_fee,
              unpacking_removal_fee)

Problem is I would then have to save these return values in anothe similar set of variables on the function call. That doesn't look very elegant and uses a lot of variables.

I do need each variables value as I need to later print them out to console based on certain criteria.

Whats the best way to retun a lot of variables value.

Baktaawar
  • 7,086
  • 24
  • 81
  • 149
  • 2
    Do all of these values have a common goal? If so, maybe create a class that holds these values and populate a new instance in the function and return it. – Shimon Cohen Aug 17 '20 at 10:29
  • 1
    return them as part of a dict? Or if it's part of a class, save them as attributes and access directly from the instance, depending on your design – Tomerikoo Aug 17 '20 at 10:29
  • 3
    Looks like a job for [named tuples](https://docs.python.org/3/library/collections.html#collections.namedtuple) or [data classes](https://docs.python.org/3/library/dataclasses.html). – bereal Aug 17 '20 at 10:29

2 Answers2

0

IMO, this usually means your function is doing too much you might want to break it down to several functions or a Class. if you still decide you want to use a single function, I'd suggest using a namedtuple to return you values in a manner you could refer to them by name.

Mr. Nun.
  • 775
  • 11
  • 29
0

You need a dataclass. Pick one which suits best for you:

Gram
  • 341
  • 2
  • 10