ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
def log(prefix):
def deco(fn):
def w(*a):
return prefix + str(fn(*a))
return w
return deco
@log("A:")
@log("B:")
def two():
return 2
print(two())- A. B:A:2
- B. A:2
- C. B:2
- D. A:B:2
Correct answer: D. A:B:2
Decorators apply bottom-up, so two = log('A:')(log('B:')(two)). The outer wrapper (A) calls the inner wrapper, which returns 'B:2', and then prefixes 'A:'.
More Python Programming questions
- What is printed? python import functools def once(fn): done = [] @functools.wraps(fn) def w(a): if not done: done.append(fn(a)) return…
- What is printed? python class A: def init(self): print("A", end=" ") class B(A): def init(self): print("B", end=" ") class C(B): pass C()…
- 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…
- What is printed? python class A: x = 1 class B(A): pass class C(A): x = 3 class D(B, C): pass B.x = 2 del B.x print(D.x, D.mro.index(C))
- What is printed? python class Shape: sides = 0 def describe(self): return f"{type(self).name}:{self.sides}" class Tri(Shape): sides = 3…
- What is printed? python class Money: def init(self, v): self.v = v def lt(self, o): return self.v < o.v def repr(self): return…