ProgrammingVery hardProduct company OA
What is printed?
What is printed?
class A:
def __eq__(self, o):
print("A", end=" ")
return NotImplemented
class B:
def __eq__(self, o):
print("B", end=" ")
return NotImplemented
print(A() == B(), end="; ")
print(A() != B())- A. A False; A True
- B. A B False; A B True
- C. A B NotImplemented; A B NotImplemented
- D. A B False; True
Correct answer: B. A B False; A B True
a == b first tries A.__eq__; NotImplemented means 'ask the other side', so the reflected B.__eq__ runs. When both decline, == falls back to identity, False. For != neither class defines __ne__, and the inherited object.__ne__ calls __eq__ and inverts it unless it gets NotImplemented — so A's and then B's __eq__ are consulted again, and the final fallback for != is 'not identical': True.
More Python Programming questions
- What is printed? python class N: def add(self, o): return "N.add" def radd(self, o): return "N.radd" class S(N): def radd(self, o): return…
- What is printed? python class C: k = 5 def f(self, v=k): return v def g(self): try: return k except NameError: return "NE" C.k = 9…
- What is printed? python d = {"a": 1} print(d.get("b", 0), "a" in d, 1 in d, d.get("b"))
- What is printed? python print(list("ab") + ["c"], tuple([1, 2]) 2)
- What is printed? python d = {"a": 1} try: d["k"] except KeyError as e: print(repr(e), e.args, len(d))
- What is printed? python print([x if x % 2 else -x for x in range(5)], [x for x in range(5) if x % 2 or x == 0])