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)
  1. A. 0 99 0
  2. B. 0 -5 -5
  3. C. None 99 0
  4. 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

Exams that ask this topic