0

Let's say I have a Python constructor

def foo(a, b, c):
     __def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

and I have a list of inputs

bar = [a, b, c]

Is there a way I can call the constructor with just a list of inputs? Eg,

foobar = foo(bar)

Or would I need to make an entire wrapper function, such as:

def fooWrapper(inputList):
   a = inputList[0]
   b = inputList[1]
   c = inputList[2]
   return foo(a, b, c)

Assuming I cannot change neither the constructor nor the way I receive the inputs (as a list), is there a more simple way to do this than creating a wrapper?

Thank you.

AMC
  • 2,642
  • 7
  • 13
  • 35
NicholasTW
  • 85
  • 1
  • 10
  • 3
    You mean something like `foo(*bar)`? Also note that your code is not valid Python – Dani Mesejo Mar 28 '20 at 17:59
  • 1
    As an aside, variable and function names should generally follow the `lower_case_with_underscores` style. – AMC Mar 28 '20 at 18:19
  • Does this answer your question? [What does \*\* (double star/asterisk) and \* (star/asterisk) do for parameters?](https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters) – Chris Mar 28 '20 at 18:23

1 Answers1

1

This should do the work for the wrapper function:

def f(a, b, c):
  return sum([a, b, c])

bar = [3, 2, 1]

total = f(*bar)

print(total)

Outputs 6.

Since a constructor can not return a value, yes you need a wrapper.

emremrah
  • 1,733
  • 13
  • 19