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
try:
    s = {P(1), P(1)}
    print(len(s))
except TypeError:
    print('TE', P(1) == P(1))
  1. A. TE False
  2. B. 1
  3. C. TE True
  4. D. 2

Correct answer: C. TE True

Defining __eq__ without __hash__ sets __hash__ to None, so putting instances in a set raises TypeError; the == itself returns True.

More Python Programming questions

Exams that ask this topic