使 python 用户定义的类可排序、可散列
在 python 中使用户定义的类可排序和/或可散列时,需要重写/实现哪些方法?
有哪些需要注意的问题?
我在解释器中输入 dir({})
以获取内置字典的方法列表。其中,我假设我需要实现一些子集,
['__cmp__', '__eq__', '__ge__', '__gt__', '__hash__', '__le__', '__lt__', '__ne__']
Python3 与 Python2 必须实现哪些方法有区别吗?
What methods need to be overridden/implemented when making user-defined classes sortable and/or hashable in 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. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我几乎将其发布为对其他答案的评论,但这本身就是一个答案。
为了使您的项目可排序,他们只需要实现
__lt__
。这是内置排序使用的唯一方法。仅当您确实想在类中使用比较运算符时才需要其他比较或 functools.total_ordering 。
为了使您的项目可散列,您可以像其他人指出的那样实现 __hash__ 。您还应该以兼容的方式实现
__eq__
——等效的项应该具有相同的哈希值。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__
. 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.To make your items hashable, you implement
__hash__
as others noted. You should also implement__eq__
in a compatible way -- items that are equivalent should hash the same.Python 2 和 3 之间没有任何区别。
为了可排序性:
您应该定义比较方法。这使您的物品可以排序。一般来说,您不应该更喜欢
__cmp__()
。我通常使用 functools.total_ordering 装饰器。
您应该小心,您的比较方法不会有任何副作用。 (更改对象的任何值)
对于散列:
您应该实现
__hash__()
方法。我认为最好的方法是返回 hash(repr(self)) ,这样你的哈希值就会是唯一的。There isn't any difference between Python 2 and 3.
For sortability:
You should define comparision methods. This makes your items sortable. Generally, you shouldn't prefer
__cmp__()
.I usually use functools.total_ordering decorator.
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. I think the best way is returninghash(repr(self))
, so your hash would be unique.有几种方法可以将对象标记为可排序。首先 - 丰富的比较,由一组函数定义:
也可以只定义一个函数:
如果要定义自定义 __hash__ 函数,则应定义最后一个函数。请参阅文档。
There are a few ways of marking your object sortable. First - rich comparison, defined by a set of functions:
Also it is possible to define only one function:
And the last should be defined if you want to define custom
__hash__
function. See the doc.实现
__lt__(self,other)
方法是使您的类可排序的答案。它不仅可以用于内置方法
sorted(iterable)
,还可以通过heapq
模块用于优先级队列。另外,我不喜欢python的设计,那么多
'__ge__'、'__gt__'、'__le__'、'__lt__'、'__ne__'
方法一点也不直观强>!作为对比,Java 的
Interface Comparable
(请参阅 java doc) 返回负整数、零或正整数,因为该对象小于、等于或大于指定对象,这是直接且友好!Implement
__lt__(self,other)
method is the answer to make your class sortable.It can be used for not only the built-in method
sorted(iterable)
, but also priority queue viaheapq
module.In addition, I don't like python's design, so many
'__ge__', '__gt__', '__le__', '__lt__', '__ne__'
methods are not intuitive at all !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!