ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
class C:
def __init__(self): self.d = {}
def __getattr__(self, name):
return f"<{name}>"
def __setattr__(self, name, v):
object.__setattr__(self, name, v * 2 if isinstance(v, int) else v)
c = C()
c.x = 5
print(c.x, c.y, c.d, getattr(c, "z", "dflt"))- A. 5 <y> {} dflt
- B. 10 <y> {} <z>
- C. 10 <y> {} dflt
- D. 10 AttributeError {} <z>
Correct answer: B. 10 <y> {} <z>
__getattr__ runs only when normal lookup fails, so c.x finds the stored 10 (doubled by __setattr__). getattr with a default only uses it if AttributeError is raised, but __getattr__ returns '<z>' instead.
More Python Programming questions
- What is printed? python class Temp: def init(self): self.c = 0 @property def c(self): return self.c @c.setter def c(self, v): if v < -273…
- What is printed? (Output lines are shown separated by " / ".) python class A: @staticmethod def s(): return "s" @classmethod def c(cls)…
- What is printed? python class Count: total = 0 def init(self): self.total += 1 Count.total += 10 a = Count(); b = Count() print(a.total…
- What is printed? python class Base: def initsubclass(cls, prefix="x", kw): super().initsubclass(kw) cls.tag = prefix + cls.name class…
- What is printed? python class It: def init(self): self.i = 0 def iter(self): return self def next(self): self.i += 1 if self.i > 3: raise…
- What is printed? python class Ctx: def init(self, n): self.n = n def enter(self): print("in", self.n, end=" ") return self.n 10 def…