special method

A Python method with a double-underscore name, such as __eq__ or __hash__, that the language calls automatically for built-in operations.

In more detail

Writing __eq__ defines what == means for your class, __hash__ what hash() returns, and __contains__ what the `in` operator does. Python calls them for you, so a class that defines them works with dicts, sets and sorting like a built-in type. Defining __eq__ without __hash__ sets __hash__ to None, making instances unhashable.

Related terms

Taught in

References