ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
import functools
def once(fn):
done = []
@functools.wraps(fn)
def w(*a):
if not done:
done.append(fn(*a))
return done[0]
return w
@once
def init(v):
print("init", v, end=" ")
return v * 3
print(init(2), init(5), init.__wrapped__(1))- A. init 2 6 6 3
- B. init 2 init 1 6 6 3
- C. init 2 init 5 6 15 3
- D. init 2 init 1 6 15 3
Correct answer: B. init 2 init 1 6 6 3
The first call runs init(2) and caches 6; init(5) returns the cached 6 without calling. functools.wraps sets __wrapped__ to the original function, which runs normally (printing init 1). All prints happen before print outputs its arguments.
More Python Programming questions
- 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…
- What is printed? python class A: def eq(self, o): print("A.eq", end=" ") return NotImplemented class B(A): def eq(self, o): print("B.eq"…