ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
def outer(fn):
print("deco", end=" ")
return fn
@outer
def g():
print("g", end=" ")
print("start", end=" ")
g(); g()- A. start deco g g
- B. start deco g deco g
- C. deco start g g
- D. deco start g deco g
Correct answer: C. deco start g g
A decorator runs once, at function-definition time (when the def statement executes), not on each call; outer returns fn unchanged, so later calls just print g.
More Python Programming questions
- What is printed? python def a(fn): def w(): return "a(" + fn() + ")" return w def b(fn): def w(): return "b(" + fn() + ")" return w @a @b…
- 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)…