ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
class S:
__slots__ = ("a",)
class T(S):
pass
t = T()
t.a = 1
t.b = 2
s = S()
try:
s.b = 2
except AttributeError:
print("AE", end=" ")
print(hasattr(t, "__dict__"), hasattr(s, "__dict__"))- A. AE True False
- B. AE False False
- C. True False
- D. AE True True
Correct answer: A. AE True False
__slots__ removes the instance __dict__ only if every class in the hierarchy defines slots. T does not declare __slots__, so its instances get a __dict__ and accept b; S instances still reject new attributes.
More Python Programming questions
- What is printed? python class Account: rate = 0.05 def init(self, bal): self.bal = bal def interest(self): return self.bal self.rate a =…
- What is printed? python def countergen(): c = 0 while True: cmd = yield c if cmd == "reset": c = 0 else: c += 1 g = countergen()…
- What is printed? python import itertools c = itertools.count() data = ["a", "b", "c"] pairs = list(zip(c, data)) print(pairs, next(c)…
- What is printed? (Output lines are shown separated by " / ".) python def f(x): return x 2 g = f del f print(g(3), g.name) try: f(1) except…
- What is printed? python x = [1, 2, 3] it = iter(x) next(it) x.insert(0, 0) print(next(it), list(it))
- What is printed? python words = ["apple", "Banana", "cherry", "date"] bylen = {} for w in words: bylen.setdefault(len(w), []).append(w)…