5.8. OOP Equality

5.8.1. SetUp

>>> from dataclasses import dataclass

5.8.2. Default

  • By default, all objects are not equal

>>> class User:
...     pass
>>> a = User()
>>> b = User()
>>> a == b
False

5.8.3. Initializer Method

  • Both object are not equal

>>> class User:
...     def __init__(self, firstname, lastname):
...         self.firstname = firstname
...         self.lastname = lastname
>>> a = User('Mark', 'Watney')
>>> b = User('Mark', 'Watney')
>>> a == b
False

5.8.4. Equality Method

  • Both object are not equal

Mind, that the following implementation of __eq__ method is erroneous and should not be used.

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

5.8.5. Equality Problem

  • Both object are not equal

Mind, that the following implementation of __eq__ method is erroneous and should not be used.

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

5.8.6. Correct Equality Method

  • Both object are not equal

The following implementation of __eq__ method is correct and can be used.

>>> 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
>>>
>>>
>>> class Admin:
...     def __init__(self, firstname, lastname):
...         self.firstname = firstname
...         self.lastname = lastname
>>> a = User('Mark', 'Watney')
>>> b = User('Mark', 'Watney')
>>> c = Admin('Mark', 'Watney')
>>> a == b
True
>>> a == c
False

5.8.7. Dataclasses

  • By default eq=True (if not specified)

  • Objects of the same type are equal

  • Objects of different types are not equal

>>> @dataclass
... class Fruit:
...     name: str
>>> @dataclass
... class Company:
...     name: str
>>> a = Fruit('Apple')
>>> b = Fruit('Apple')
>>> c = Fruit('Company')
>>> a == b
True
>>> a == b
False

5.8.8. Dataclasses NotEqual

  • Setting eq=False will disable the equality check

  • Objects of the same type are not equal

  • Objects of different types are not equal

>>> @dataclass(eq=False)
... class Fruit:
...     name: str
>>> @dataclass
... class Company:
...     name: str
>>> a = Fruit('Apple')
>>> b = Fruit('Apple')
>>> c = Fruit('Company')
>>> a == b
False
>>> a == b
False

5.8.9. Assignments