使 python 用户定义的类可排序、可散列

发布于 2024-12-01 05:56:12 字数 286 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

栀子花开つ 2024-12-08 05:56:12

我几乎将其发布为对其他答案的评论,但这本身就是一个答案。

为了使您的项目可排序,他们只需要实现__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.

怕倦 2024-12-08 05:56:12

Python 2 和 3 之间没有任何区别。

为了可排序性:

您应该定义比较方法。这使您的物品可以排序。一般来说,您不应该更喜欢 __cmp__()

我通常使用 functools.total_ordering 装饰器。

functools.total_ordering(cls) 给定一个定义一个或多个丰富的类
比较排序方法,该类装饰器提供其余部分。
这简化了指定所有可能的工作量
丰富的比较操作:

该类必须定义 __lt__()__le__()__gt__() 之一,或者
__ge__()。此外,该类应提供一个 __eq__() 方法。

您应该小心,您的比较方法不会有任何副作用。 (更改对象的任何值)

对于散列:

您应该实现__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.

functools.total_ordering(cls) Given a class defining one or more rich
comparison ordering methods, this class decorator supplies the rest.
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__(). In addition, the class should supply an __eq__() method.

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 returning hash(repr(self)), so your hash would be unique.

雨后咖啡店 2024-12-08 05:56:12

有几种方法可以将对象标记为可排序。首先 - 丰富的比较,由一组函数定义:

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

也可以只定义一个函数:

object.__cmp__(self, other)

如果要定义自定义 __hash__ 函数,则应定义最后一个函数。请参阅文档

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. See the doc.

从此见与不见 2024-12-08 05:56:12

实现 __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 via heapq 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文