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!
7 Respuestas
+ 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
+ 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)
+ 7
You're welcome Dingo I hope it is clear now
+ 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
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?
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?
0
Thank you Uni