From the course: Advanced Python (2018)

Unlock the full course today

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

Object comparisons

Object comparisons

- [Instructor] It is also possible to implement comparison operators using our special class methods to allow objects to compare themselves to other objects of the same type. So, this table lists the names of the special class function that you can override to provide comparisons, and you can see that there are methods for greater than, less than, greater or less than or equal, and so on. Each of these methods compares the object that the method is being called on to the object specified by the parameter named other, and you can see the type of comparison expression that triggers that particular function in the right-hand column. So let's take these for a test drive. The code for this example is in comparison underscore start dot py in the classes folder, and at the top of the file you can see that I have a class definition for employee, and that it specifies a first and last name, an employee level, and years of service. I also have placeholder functions for the various comparison…

Contents