equivalence

Why does `if None.__eq__("a")` seem to evaluate to True (but not quite)?

Why does `if None.__eq__("a")` seem to evaluate to True (but not quite)? Question: If you execute the following statement in Python 3.7, it will (from my testing) print b: if None.__eq__(“a”): print(“b”) However, None.__eq__(“a”) evaluates to NotImplemented. Naturally, “a”.__eq__(“a”) evaluates to True, and “b”.__eq__(“a”) evaluates to False. I initially discovered this when testing the return …

Total answers: 4

Elegant ways to support equivalence ("equality") in Python classes

Elegant ways to support equivalence ("equality") in Python classes Question: When writing custom classes it is often important to allow equivalence by means of the == and != operators. In Python, this is made possible by implementing the __eq__ and __ne__ special methods, respectively. The easiest way I’ve found to do this is the following …

Total answers: 11