What is the standard way of making a class comparable in Python 3? (For example, by id.)
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
To make classes comparable, you only need to implement
__lt__and decorate the class withfunctools.total_ordering. You should also provide an__eq__method if possible. This provides the rest of the comparison operators so you don’t have to write any of them yourself.