__cmp__ 方法在 Python 2.x 中是否无法按预期工作?
class x:
def __init__(self,name):
self.name=name
def __str__(self):
return self.name
def __cmp__(self,other):
print("cmp method called with self="+str(self)+",other="+str(other))
return self.name==other.name
# return False
instance1=x("hello")
instance2=x("there")
print(instance1==instance2)
print(instance1.name==instance2.name)
这里的输出是:
cmp method called with self=hello,other=there
True
False
这不是我所期望的:我试图说“如果名称字段相等,则两个实例相等”。
如果我只是从 __cmp__
函数返回 False
,它也会报告为 True
! 如果我返回 -1
,那么我会得到 False
- 但由于我正在尝试比较字符串,这感觉不对。
我在这里做错了什么?
class x:
def __init__(self,name):
self.name=name
def __str__(self):
return self.name
def __cmp__(self,other):
print("cmp method called with self="+str(self)+",other="+str(other))
return self.name==other.name
# return False
instance1=x("hello")
instance2=x("there")
print(instance1==instance2)
print(instance1.name==instance2.name)
The output here is:
cmp method called with self=hello,other=there
True
False
Which is not what I expected: I'm trying to say 'two instances are equal if the name fields are equal'.
If I simply return False
from the __cmp__
function, this reports as True
as well!!
If I return -1
, then I get False
- but since I'm trying to compare strings, this doesn't feel right.
What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果
x
,如果__cmp__(x,y)
应返回负数(例如 -1)。 yx > 则为正数(例如1)。如果
和 0。你永远不应该用它返回一个布尔值。x == y
,则为 y您重载的是
__eq__(x, y)
。__cmp__(x,y)
should return a negative number (e.g. -1) ifx < y
, a positive number (e.g. 1) ifx > y
and 0 ifx == y
. You should never return a boolean with it.What you're overloading is
__eq__(x, y)
.当 self <; 时,
__cmp__
方法应返回 -1、0 或 1。其他,自我==其他,自我>其他恭敬地。您可以
在代码中执行以获得正确的结果
the
__cmp__
method should return -1, 0 or 1, when self < other, self == other, self > other respectvelly.You can do
in your code for a proper result
您将
__cmp__
与__eq__
混淆了。来自
__cmp__
的文档:__eq__
返回一个布尔值,确定两个对象是否相等,__cmp__
返回一个整数,确定两个对象是否大于或小于彼此,因此被调用,除非您具有特定的__eq__
、__ne__
、__le__
、__ge__
、__lt__
和__gt__
方法。在您的情况下,您确实需要一个
__cmp__
方法,而不是__eq__
,因为它会节省您实现其他比较的其他 5 种方法。您可以使用
cmp()
函数 并将在您的__cmp__
方法中遵循以下内容:注意,正如 Ignacio 所强调的那样,这是 不是 Python 3.0 中的首选方法,但在 Python 2.x 中
__cmp__
是可行的方法。You're confusing
__cmp__
with__eq__
.From the documentation of
__cmp__
:__eq__
returns a boolean which determines if two objects are equal,__cmp__
returns an integer which determines if the two objects are greater or less than each other, and so is called unless you have specific__eq__
,__ne__
,__le__
,__ge__
,__lt__
and__gt__
methods.In your case you do want a
__cmp__
method rather than__eq__
as it will save you implementing the other 5 methods for the other comparisons.You could use the
cmp()
function and put the following in your__cmp__
method:Note, as highlighted by Ignacio is this isn't the preferred method in Python 3.0, but in Python 2.x
__cmp__
is the way to go.__cmp__()
已过时。而是定义__lt__()
、__eq__()
和__gt__()
。即便如此,你还是做错了。你应该返回一个整数。
__cmp__()
is obsolescent. Define__lt__()
,__eq__()
, and__gt__()
instead.Even so, you're doing it wrong. You're supposed to return an integer.
查找
__cmp__
的文档,你就可以了应该返回一个整数:Lookup the documentation for
__cmp__
, youre supposed to return an integer: