user96769 user96769 - 16 days ago 31
Python Question

Parsing return value from one method as two arguments for another function?

I have a function

def findSpace(aBookcase):
which returns
return row,column
and another function which takes
def setName(self, row, column, name):
.

Now, when I execute
aBookcase.setName(findSpace(aBookcase), name)
I get
TypeError: setName() takes exactly 4 arguments (3 given)
.

How can I make it parse the result from findSpace() as two arguments to function setName(), without changing findSpace(), because I have other code already depend on it

Answer

findSpace returns a tuple. That is one object. aBookcase.setName expects three separate arguments, in addition to the instance itself. Unpack the result and then use the arguments:

r,c = findSpace(aBookcase)
aBookcase.setName(r, c, name)

In Python 3.5 or higher, you can simply use *:

aBookcase.setName(*findSpace(aBookcase), name)