0

This question should be really easy to answer, however I didn't find anything/don't know how to search.

def testcall(function, argument):
    function(*argument)

testcall(print, "test")
# output:
t e s t

why t e s t and not test?

speendo
  • 13,045
  • 22
  • 71
  • 107
  • 2
    `*argument` is breaking the string into an array of 4 chars, so you're calling `print 't', 'e', 's', 't'`; you should declare `argument` as `*argument` – vanza Feb 06 '14 at 21:11

2 Answers2

4

You are using the splat syntax (*argument) to break up argument into individual characters. Then, you are passing these characters to print. It is no different than doing:

>>> print('t', 'e', 's', 't')
t e s t
>>>

Remove the * to fix the problem:

>>> def testcall(function, argument):
...     function(argument)
...
>>> testcall(print, "test")
test
>>>
Community
  • 1
  • 1
4

Your splats are asymmetric. It should be:

def testcall(function, *argument):
    function(*argument)

In general, if you want your function to behave like another function, it should accept a splat and send a splat. This answer explains the general case

Community
  • 1
  • 1
Cuadue
  • 3,769
  • 3
  • 24
  • 38