[英]Making a python user-defined class sortable, hashable
What methods need to be overridden/implemented when making user-defined classes sortable and/or hashable in python? 在python中使用户定义的类可排序和/或可清除时,需要覆盖/实现哪些方法?
What are the gotchas to watch out for? 有什么值得注意的?
I type dir({})
into my interpreter to get a list of methods on built-in dicts. 我在我的解释器中键入
dir({})
以获取内置dicts的方法列表。 Of those, I assume I need to some implement some subset of 其中,我假设我需要实现一些子集
['__cmp__', '__eq__', '__ge__', '__gt__', '__hash__', '__le__', '__lt__', '__ne__']
Is there a difference in which methods must be implemented for Python3 as opposed to Python2? 与Python2相比,Python3必须实现哪些方法有区别?
I almost posted this as a comment to the other answers but it's really an answer in and of itself. 我几乎把它作为对其他答案的评论发布,但它本身就是一个答案。
To make your items sortable, they only need to implement __lt__
. 要使您的项目可排序,他们只需要实现
__lt__
。 That's the only method used by the built in sort. 这是内置排序使用的唯一方法。
The other comparisons or functools.total_ordering
are only needed if you actually want to use the comparison operators with your class. 只有在您真正想要将比较运算符与您的类一起使用时,才需要进行其他比较或
functools.total_ordering
。
To make your items hashable, you implement __hash__
as others noted. 为了使您的项目可以
__hash__
,您__hash__
像其他人注意到的那样实现__hash__
。 You should also implement __eq__
in a compatible way -- items that are equivalent should hash the same. 您还应该以兼容的方式实现
__eq__
- 等效的项应该使用相同的哈希值。
There isn't any difference between Python 2 and 3. Python 2和3之间没有任何区别。
For sortability: 可分类性:
You should define comparision methods. 您应该定义比较方法。 This makes your items sortable.
这使您的商品可以排序。 Generally, you shouldn't prefer
__cmp__()
. 通常,您不应该更喜欢
__cmp__()
。
I usually use functools.total_ordering decorator. 我通常使用functools.total_ordering装饰器。
functools.total_ordering(cls) Given a class defining one or more rich comparison ordering methods, this class decorator supplies the rest.
functools.total_ordering(cls)给定一个定义一个或多个丰富的比较排序方法的类,这个类装饰器提供其余的。 This simplifies the effort involved in specifying all of the possible rich comparison operations:
这简化了指定所有可能的丰富比较操作所涉及的工作:
The class must define one of
__lt__()
,__le__()
,__gt__()
, or__ge__()
.该类必须定义
__lt__()
,__le__()
__lt__()
,__le__()
__gt__()
或__ge__()
。 In addition, the class should supply an__eq__()
method.此外,该类应提供
__eq__()
方法。
You should be careful that your comparison methods do not have any side effects. 你应该小心你的比较方法没有任何副作用。 (change any of the values of the object)
(更改对象的任何值)
For hashing: 对于散列:
You should implement __hash__()
method. 你应该实现
__hash__()
方法。 I think the best way is returning hash(repr(self))
, so your hash would be unique. 我认为最好的方法是返回
hash(repr(self))
,所以你的哈希值是唯一的。
There are a few ways of marking your object sortable. 有几种方法可以将对象标记为可排序。 First - 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)
Also it is possible to define only one function: 还可以只定义一个函数:
object.__cmp__(self, other)
And the last should be defined if you want to define custom __hash__
function. 如果要定义自定义
__hash__
函数,则应定义最后一个。 See the doc . 见文档 。
Implement __lt__(self,other)
method is the answer to make your class sortable. 实现
__lt__(self,other)
方法是让您的类可排序的答案。
It can be used for not only the built-in method sorted(iterable)
, but also priority queue via heapq
module. 它不仅可用于内置方法
sorted(iterable)
,还可用于通过heapq
模块的优先级队列。
In addition, I don't like python's design, so many '__ge__', '__gt__', '__le__', '__lt__', '__ne__'
methods are not intuitive at all ! 另外,我不喜欢python的设计,所以很多
'__ge__', '__gt__', '__le__', '__lt__', '__ne__'
方法根本不直观 !
As a contrast, Java's Interface Comparable<T>
(see java doc ) returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object, which is direct and friendly ! 相比之下,Java的
Interface Comparable<T>
(请参阅java doc )返回负整数,零或正整数,因为此对象小于,等于或大于指定对象,这是直接且友好的 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.