ProgrammingHardTCS NQT · Wipro pattern

What is printed?

What is printed?
class P:
    def __init__(self, x):
        self.x = x
    def __eq__(self, o):
        return self.x == o.x
try:
    s = {P(1)}
except TypeError:
    print('unhashable')
  1. A. True
  2. B. unhashable
  3. C. False
  4. D. no error, the set has one item

Correct answer: B. unhashable

Defining __eq__ without __hash__ sets __hash__ to None in the class, so instances cannot go into a set or be used as dict keys. The set literal raises TypeError.

More Python Programming questions

Exams that ask this topic