ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
import functools
def deco(fn):
@functools.wraps(fn)
def wrapper(*a):
wrapper.n += 1
return fn(*a)
wrapper.n = 0
return wrapper
@deco
def fib(n):
return n if n < 2 else fib(n - 1) + fib(n - 2)
print(fib(5), fib.n, fib.__name__)- A. 5 1 fib
- B. 5 15 wrapper
- C. 8 15 fib
- D. 5 15 fib
Correct answer: D. 5 15 fib
The recursive calls inside fib go through the global name fib, which is now the wrapper, so every call is counted: fib(5) makes 15 calls in total. wraps copies __name__.
More Python Programming questions
- What is printed? python def repeat(n): print("R", n, end=" ") def deco(fn): def w(x): return [fn(x) for in range(n)] return w return deco…
- What is printed? python calls = [] def trace(fn): calls.append(fn.name) return fn class C: @trace def m(self): pass @staticmethod @trace…
- What is printed? python def memo(fn): cache = {} def w(n): if n not in cache: cache[n] = fn(n) return cache[n] return w @memo def f(n)…
- What is printed? python def register(cls): cls.tag = cls.name.lower() return cls @register class Alpha: tag = "none" class Beta(Alpha)…
- What is printed? python class A: def hi(self): return "A" class B(A): pass class C(A): def hi(self): return "C" class D(B, C): pass…
- What is printed? python class A: def f(self): return "A" class B(A): def f(self): return "B" + super().f() class C(A): def f(self): return…