Creating a python user-defined class, sortable, hashable

What methods do you need to override / implement when creating custom classes that are sortable and / or hashed in python?

Why do we need sentries?

I type dir({}) in my interpreter to get a list of methods on inline dicts. Of these, I assume that I need some to implement some subset

 ['__cmp__', '__eq__', '__ge__', '__gt__', '__hash__', '__le__', '__lt__', '__ne__'] 

Is there a difference in which methods should be implemented for Python3, not Python2?

+60
python sorting class magic-methods hash
Aug 22 '11 at 19:25
source share
4 answers

I almost posted this as a comment on the other answers, but it really is an answer in itself.

To sort the elements, they need to implement __lt__ . This is the only method used by inline sorting.

Other comparisons or functools.total_ordering are only needed if you really want to use comparison operators with your class.

To make your elements hashable, you implement __hash__ , as others have noted. You should also implement __eq__ compatible way - elements equivalent should have the same thing.

+68
Aug 22 '11 at 19:50
source share

There is no difference between Python 2 and 3.

To sort:

You must define comparison methods. It makes your items sortable. Generally, you should not prefer __cmp__() .

I usually use functools.total_ordering decorator.

functools.total_ordering (cls) Given a class that defines one or more rich comparison ordering methods, this class decorator provides the rest. This simplifies the effort to identify all the possible advanced comparison operations:

The class must define one of __lt__() , __le__() , __gt__() or __ge__() . In addition, the class must provide __eq__() .

You must be careful that your comparison methods do not have side effects. (change any of the values โ€‹โ€‹of the object)

For stirring:

You must implement __hash__() . I think the best way is to return hash(repr(self)) , so your hash will be unique.

+15
Aug 22 '11 at 19:38
source share

There are several ways to sort your object. The first is a rich comparison defined by a set of functions:

 object.__lt__(self, other) object.__le__(self, other) object.__eq__(self, other) object.__ne__(self, other) object.__gt__(self, other) object.__ge__(self, other) 

You can also define only one function:

 object.__cmp__(self, other) 

And the latter should be defined if you want to define a custom __hash__ function. See doc .

+2
Aug 22 '11 at 19:35
source share

Implement the __lt__(self,other) method is the answer to make your class sortable.
It can be used not only for the built-in sorted(iterable) method, but also for the priority queue through the heapq module.

Also, I don't like the python design, so many of the methods '__ge__', '__gt__', '__le__', '__lt__', '__ne__' not intuitive !
Unlike the Java Interface Comparable<T> (see java doc ) returns a negative integer, zero or a positive integer, since this object is smaller, equal or larger than the specified object, which is direct and friendly !

-one
Oct 16 '17 at 7:58
source share



All Articles