ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
def outer(fn):
def w(*a, **k):
return fn(*a, **k) * 2
return w
class C:
@outer
def val(self, x):
return x + 1
@property
@outer
def p(self):
return 5
c = C()
print(c.val(1), c.p, C.val.__name__)- A. 4 10 val
- B. 4 10 w
- C. 2 10 w
- D. 4 5 w
Correct answer: B. 4 10 w
The wrapper is an ordinary function stored on the class, so it binds like a method: self is passed through *a. property wraps the already-decorated getter, doubling 5. Without wraps the name is w.
More Python Programming questions
- What is printed? python def log(prefix): def deco(fn): def w(a): return prefix + str(fn(a)) return w return deco @log("A:") @log("B:") def…
- 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…