ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
class Celsius:
def __init__(self, t=0): self.t = t
def __get__(self, obj, owner):
return self if obj is None else obj._c
def __set__(self, obj, v):
obj._c = round(v, 1)
class Room:
temp = Celsius()
r1, r2 = Room(), Room()
r1.temp = 21.66
r2.temp = 19
print(r1.temp, r2.temp, type(Room.temp).__name__)- A. 21.66 19 Celsius
- B. 19 19 Celsius
- C. 21.7 19 Celsius
- D. 21.7 19 float
Correct answer: C. 21.7 19 Celsius
The descriptor instance is shared by the class, but it stores each value on the owning object (obj._c), so rooms do not interfere. Accessed through the class (obj is None) it returns itself.
More Python Programming questions
- What is printed? python class Vec: def init(self, c): self.c = c def mul(self, k): return Vec((x k for x in self.c)) rmul = mul def…
- What is printed? python class Ordered: def init(self, v): self.v = v def lt(self, o): print("lt", end=" ") return self.v < o.v def…
- What is printed? python import functools @functools.totalordering class V: def init(self, n): self.n = n def eq(self, o): return self.n ==…
- What is printed? python def f(): l = [] for i in range(3): try: if i == 0: raise ValueError l.append(i) except ValueError: l.append("v")…
- What is printed? python def risky(n): if n == 1: raise KeyError(n) if n == 2: raise IndexError(n) return n res = [] for n in range(4)…
- What is printed? python class E(Exception): def str(self): return "custom" try: try: raise E("orig") except E as e: raise…