ProgrammingVery hardPython · GATE DA · Advanced

What is printed?

What is printed?
class P:
    def __init__(self, v): self.v = v
    def __eq__(self, o): return self.v == o.v
s = set()
try:
    s.add(P(1))
except TypeError as e:
    print('TypeError')
print(P.__hash__)
  1. A. no error / None
  2. B. TypeError / <function P.__hash__>
  3. C. TypeError / None
  4. D. <slot wrapper '__hash__' of 'object' objects>

Correct answer: C. TypeError / None

Defining __eq__ without __hash__ sets __hash__ to None implicitly, so instances are unhashable and set.add raises TypeError.

More Python Programming questions

Exams that ask this topic