0

Function argument

def test(func, arg): return func(func(arg)) def mult(x): return x*x print(test(mult,2)) What is the output and how/what steps did it first take get to the conclusion? Thanks for the assist and any tips for future reference on how tackle these type of functions would be great!

17th Jan 2019, 11:15 AM
Dingo
Dingo - avatar
7 ответов
+ 10
So test(mult, 2) So test is taking a function func with an argument arg. In this case the function func is mult and the argument is 2: test returns func(func(arg)) so test(mult, 2) will return mult(mult (2)) which is basically: mult(2) is 2*2=4 so mult(mult(2))=mult(4) which will be 4*4=16 I hope this helps
17th Jan 2019, 11:21 AM
Uni
Uni - avatar
+ 7
mult(mult, 2) is not correct because mult needs only 1 argument. test however, takes 2 arguments (hence test(mult, 2)) So first the most inner function (if I can call it like this) will be evaluated ("executed") first (as it is an argument). So in mult(mult(2)) you'll first have the mult(2) executed first then the last multiple function will be evaluated with the argument mult(2)
17th Jan 2019, 11:36 AM
Uni
Uni - avatar
+ 7
You're welcome Dingo I hope it is clear now
17th Jan 2019, 11:37 AM
Uni
Uni - avatar
+ 6
Yes func is the argument of itself and of test and arg is only the argument of func. mult did not become 2. You have test(mult, 2)=mult(mult(2)) so mult 2 is 2*2=4 and mult(mult(2))=mult(4)=4*4=16
17th Jan 2019, 11:32 AM
Uni
Uni - avatar
0
kinda lost me on the 3rd line, func(func(arg)) #so func and arg are arguments? and when print(test(mult,2) does it start of from test as making func(func(arg)) to mult(mult, 2) but the thing that got me is... how did mult become 2? as you mentioned 2*2 =4 and 4*4 = 16?
17th Jan 2019, 11:29 AM
Dingo
Dingo - avatar
0
nvm, i just got it... but which one goes first... the inner argument or the other one goes first... for example. mult(mult, 2) will the mult ourside excute first or the inner one? and what does the 2 represent?
17th Jan 2019, 11:31 AM
Dingo
Dingo - avatar
0
Thank you Uni
17th Jan 2019, 11:35 AM
Dingo
Dingo - avatar