PDA

View Full Version : [SOLVED] python takes wrong number of arguments



qrwe
August 6th, 2010, 10:03 AM
Hello,

Consider:

Function funOne takes 1 argument and returns 2 (in a tuple)
Function funTwo takes 2 arguments (in a tuple) and returns 1


When I run funTwo(funOne(arg)), I get:


>>> funTwo(funOne(arg))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: funTwo() takes exactly 2 arguments (1 given)


How do I make python understand that it actually does get two arguments, but from another function?
Thanks for any help!

slooksterpsv
August 6th, 2010, 10:16 AM
Hello,

Consider:

Function funOne takes 1 argument and returns 2 (in a tuple)
Function funTwo takes 2 arguments (in a tuple) and returns 1


When I run funTwo(funOne(arg)), I get:


>>> funTwo(funOne(arg))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: funTwo() takes exactly 2 arguments (1 given)


How do I make python understand that it actually does get two arguments, but from another function?
Thanks for any help!

You could call it like so:
funTwo(funOne(arg)[0],funOne(arg)[1])

DaithiF
August 6th, 2010, 10:37 AM
Hi,
use the star operator '*'

funTwo(*funOne(arg))

simeon87
August 6th, 2010, 11:04 AM
Hi,
use the star operator '*'

funTwo(*funOne(arg))


One day, you'll wish you had this operator in some other programming language.

spupy
August 6th, 2010, 06:47 PM
You could call it like so:
funTwo(funOne(arg)[0],funOne(arg)[1])

This seems wrong! Why call the function twice?

@qrwe
Can we see the signatures of the functions?


EDIT: Ah, yes, the star operator. That might be the answer.

Bachstelze
August 6th, 2010, 07:14 PM
The star operator is indeed what you want, but you're confused about something:




Function funOne takes 1 argument and returns 2 (in a tuple)


In most (perhaps all) programming language, a function always returns exactly one value. In this case, you are indeed returning one value: the tuple. If you want to pass the values contained in the tuple as arguments to another function, you have to "unpack" it, which is indeed what the star operator does.

nvteighen
August 6th, 2010, 08:23 PM
One day, you'll wish you had this operator in some other programming language.

Backquote and comma-splice in Common Lisp... or just use apply ;)

qrwe
August 9th, 2010, 03:37 PM
@Bachstelze @DaithiF:
Thank you very much, that made the trick! I have programmed before in languages that accepts all elements in tuples as multiple arguments, that's why I got a little confused here. :-)