10.5. Operator Equality
a == b
- will call method "eq" on objecta
(a.__eq__(b)
)a != b
- will call method "ne" on objecta
(a.__ne__(b)
)
Operator |
Method |
---|---|
|
|
|
|
10.5.1. Syntax
>>> class MyClass:
... def __eq__(self, other): ... # a == b
... def __ne__(self, other): ... # a != b
10.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
10.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
10.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
10.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('Alice', 'Apricot')
>>> b = User('Alice', 'Apricot')
>>> c = User('Bob', 'Blackthorn')
>>>
>>> a == b
True
>>>
>>> a == c
False
10.5.6. Assignments
# %% About
# - Name: Operator Comparison Equals
# - Difficulty: easy
# - Lines: 3
# - Minutes: 3
# %% 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
# %% English
# 1. Override operator ``==`` so that two `Group` objects are equal
# 2. Remember to check if `other` is instance of the same class
# 3. Run doctests - all must succeed
# %% Polish
# 1. Nadpisz operator ``==``, tak aby dwa obiekty `Group` były równe
# 2. Pamiętaj aby sprawdzić czy `other` jest instancją tej samej klasy
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'
>>> class Other:
... def __init__(self, gid, name):
... self.gid = gid
... self.name = name
>>> Group(gid=1, name='users') == Group(gid=1, name='users')
True
>>> Group(gid=1, name='users') == Group(gid=2, name='staff')
False
>>> Group(gid=1, name='users') == Group(gid=3, name='admins')
False
>>> Group(gid=1, name='users') == Other(gid=0, name='root')
False
>>> Group(gid=1, name='users') == Other(gid=1, name='users')
False
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
from typing import Callable
Group: type
__eq__: Callable[[object, object], bool]
# %% Data
# %% Result
class Group:
def __init__(self, gid, name):
self.gid = gid
self.name = name