Is it safe to just implement __lt__ for a class that will be sorted?

PEP 8 recommends against this practice. I also recommend against it because it is a fragile programming style (not robust against minor code modifications):

Instead, consider using the functools.total_ordering class decorator to do the work:

@total_ordering
class Student:
    def __eq__(self, other):
        return ((self.lastname.lower(), self.firstname.lower()) ==
                (other.lastname.lower(), other.firstname.lower()))
    def __lt__(self, other):
        return ((self.lastname.lower(), self.firstname.lower()) <
                (other.lastname.lower(), other.firstname.lower()))

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)