ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
def a(fn):
def w(): return "a(" + fn() + ")"
return w
def b(fn):
def w(): return "b(" + fn() + ")"
return w
@a
@b
def f(): return "f"
print(f(), f.__name__)- A. a(b(f)) w
- B. b(a(f)) w
- C. a(b(f)) f
- D. b(a(f)) f
Correct answer: A. a(b(f)) w
Stacked decorators apply bottom-up: f = a(b(f)), so the outermost wrapper is a's. Without functools.wraps the result is just the inner function named w.
More Python Programming questions
- What is printed? python import functools def deco(fn): @functools.wraps(fn) def wrapper(a): wrapper.n += 1 return fn(a) wrapper.n = 0…
- 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…