From the course: Python Object-Oriented Programming

Unlock the full course today

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

Solution: Compare objects

Solution: Compare objects

(upbeat music) - [Instructor] For this challenge, we needed to implement the comparison magic methods in order to make our stock objects comparable to each other. There are five methods that we need to implement. There's equal, less than, greater than, and less than or equal and greater than or equal. So here in my solution you can see that I've implemented each of these five methods, and they use the price property to determine the comparison. So let's go ahead and try this out. And here is the testing code. So we have two ticker symbols with two different prices and company descriptions. Here is the code that instantiates each one of these assets and then compares them using our operators. So let's go ahead and try this out. All right, and you can see that the code works. So let's see. Is equal is False because the prices are not the same. Is less than is True because price1 is in fact less than price2, which, of course, means that is greater than is going to be False. Is less than…

Contents