+ 3
I need help understanding what is going on with this recursion problem
Could some one explain to me how does this code out put 26 def decor(a,b): def sq(func): return func(a,b)*func(a,b) return sq def subs(a,b): return(a-b) def add(a,b): return (a+b) f = decor(2,3) print(f(add)+f(subs))
6 Respuestas
+ 7
the function decor builds a function sq.
a,b --> sq(func)
f is
f(func):
return func(2,3)*func(2,3)
f is called twice:
f(add) returns add(2,3)*add(2,3)
= 5*5
f(subs) returns
subs(2,3) * subs(2,3)
=(-1)*(-1) =1
+ 4
(2+3)**2 + (2-3)**2
+ 4
yes
+ 2
Line 3 outputs func**2. It's the same as func*func.
+ 1
I see ... is it because sq(func) = func**2
0
@oma Falk
Could you please explain to me which part of the code that outputs **2?