Comparable classes in Python 3

后端 未结 5 1324
北恋
北恋 2020-11-30 07:54

What is the standard way of making a class comparable in Python 3? (For example, by id.)

5条回答
  •  心在旅途
    2020-11-30 08:47

    To make classes comparable, you only need to implement __lt__ and decorate the class with functools.total_ordering. This provides the rest of the comparison operators so you don't have to write any of them yourself.

提交回复
热议问题