8.5. Operator Equality

  • a == b - will call method "eq" on object a (a.__eq__(b))

  • a != b - will call method "ne" on object a (a.__ne__(b))

Table 8.4. Comparison Operator Overload

Operator

Method

obj == other

obj.__eq__(other)

obj != other

obj.__ne__(other)

8.5.1. Syntax

>>> class MyClass:
...     def __eq__(self, other): ...  # a == b
...     def __ne__(self, other): ...  # a != b

8.5.2. Problem

>>> class Vector:
...     x: int
...     y: int
...
...     def __init__(self, x, y):
...         self.x = x
...         self.y = y
...
...     def __repr__(self):
...         return f'Vector(x={self.x}, y={self.y})'
>>>
>>>
>>> a = Vector(x=1, y=2)
>>> b = Vector(x=1, y=2)
>>>
>>> a == b
False

8.5.3. Solution

  • When you compare objects with the same fields from two different classes

  • Always remember to compare classes

  • This way you avoid bug, when both has the same fields and values

  • Eq Works at Both Sides

>>> class Vector:
...     x: int
...     y: int
...
...     def __init__(self, x, y):
...         self.x = x
...         self.y = y
...
...     def __repr__(self):
...         return f'Vector(x={self.x}, y={self.y})'
...
...     def __eq__(self, other):
...         return self.__class__ is other.__class__ \
...            and self.x == other.x \
...            and self.y == other.y
>>>
>>>
>>> a = Vector(x=1, y=2)
>>> b = Vector(x=1, y=2)
>>>
>>> a == b
True

8.5.4. Operator

  • operator.eq(a, b) - equal - a.__eq__(b)

  • operator.ne(a, b) - not equal - a.__ne__(b)

>>> import operator
>>> operator.eq(1, 2)
False
>>> operator.ne(1, 2)
True

8.5.5. Use Case - 1

>>> class User:
...     def __init__(self, firstname, lastname):
...         self.firstname = firstname
...         self.lastname = lastname
...
...     def __eq__(self, other):
...         return self.__class__ is other.__class__ \
...            and self.firstname == other.firstname \
...            and self.lastname == other.lastname
>>>
>>> a = User('Mark', 'Watney')
>>> b = User('Mark', 'Watney')
>>> c = User('Melissa', 'Lewis')
>>>
>>> a == b
True
>>>
>>> a == c
False

8.5.6. Assignments

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: Operator Comparison Equals
# - Difficulty: easy
# - Lines: 3
# - Minutes: 3

# %% English
# 1. Override operator for code to work correctly
# 2. Do not use `dataclasses`
# 3. Run doctests - all must succeed

# %% Polish
# 1. Nadpisz operator aby poniższy kod zadziałał poprawnie
# 2. Nie używaj `dataclasses`
# 3. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

>>> class Car:
...     def __init__(self, year, name):
...         self.year = year
...         self.name = name

>>> Mission(2035, 'Ares 3') == Car(2035, 'Ares 3')
False
>>> Mission(2035, 'Ares 3') == Mission(2035, 'Ares 3')
True
>>> Mission(2035, 'Ares 3') == Mission(1973, 'Apollo 18')
False
>>> Mission(2035, 'Ares 3') == Mission(2035, 'Apollo 18')
False
>>> Mission(2035, 'Ares 3') == Mission(1973, 'Ares 3')
False
"""

class Mission:
    def __init__(self, year, name):
        self.year = year
        self.name = name