ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
class Pos:
def __set_name__(self, owner, name):
self.name = "_" + name
def __get__(self, obj, typ=None):
return getattr(obj, self.name, 0)
def __set__(self, obj, v):
setattr(obj, self.name, max(v, 0))
class P:
x = Pos()
p = P()
before = p.x
p.x = -5
p.__dict__["x"] = 99
print(before, p.x, p._x)- A. 0 99 0
- B. 0 -5 -5
- C. None 99 0
- D. 0 0 0
Correct answer: D. 0 0 0
Pos defines __set__, so it is a data descriptor and takes priority over the instance __dict__: p.x always goes through __get__ (reading _x), ignoring the 99 stored under 'x'. The setter clamps -5 to 0.
More Python Programming questions
- What is printed? python def f(n): return [i for i in range(n) if i % 3 == 0 if i % 2 == 0] g = lambda n: [i for i in range(n) if i % 3 ==…
- What is printed? python a = {"x": 1, "y": 2} b = {v: k for k, v in a.items()} c = {k: v for k, v in zip("abc", [1, 2])} print(b, c, {1…
- What is printed? python a = b = [] a.append(1) c, d = [], [] c.append(1) x = y = 5 x += 1 print(b, d, y)
- What is printed? python a = [1, 2, 3] i = 0 i, a[i] = 2, 9 print(a, i)
- What is printed? python x = [0, 1] i = 0 x[i], i = 5, 1 j = 1 j, x[j] = 0, 7 print(x, i, j)
- What is printed? python a, b = 1, 2 a, b = b, a + b c = [3, 4] c[0], c[1] = c[1], c[0] m, n = n, m = 7, 8 print(a, b, c, m, n)