eq python

Solutions on MaxInterview for eq python by the best coders in the world

showing results for - " eq python"
Elias
04 Mar 2020
1# The __eq__ method of a class is called when using the == operator
2# You can overload it for custom == logic
3
4class Food:
5    def __init__(self, name, price):
6        self.name = name
7        self.price = price
8
9    def __eq__(self, other):
10        return self.name == other.name
11
12cheap_bread = Food('bread', 5)
13expensive_bread = Food('bread', 1000)
14
15print(cheap_bread == expensive_bread)
16# True