From the course: Python Object-Oriented Programming

Unlock the full course today

Join today to access over 24,500 courses taught by industry experts.

Equality and comparison

Equality and comparison

- [Speaker] Plain objects in Python by default don't know how to compare themselves to each other, but we can teach them how to do so by using the equality and comparison magic methods. So let's open up magic EQ start. And once again, you can see I have my book class defined with some attributes, and we've got a few variables down here that we use to create and hold some book objects. B1 and B3 both contain the same information, but watch what happens when we try to compare them to each other. So I'm going to print out B1 double equals B3. Now again, remember same values in the attributes, right? But watch what happens. I'm going to open the terminal and run. You can see that the result is false, but wait a second they have the same data, same title, right? Same author, same price. Even though all the attributes are the same, the result is false. And the reason this happens is because Python doesn't do an…

Contents