貌似diveintopython中关于__cmp__的解释不是很正确

貌似diveintopython中关于__cmp__的解释不是很正确

找了dive into python的英文版本,关于 __cmp__的解释是这样的:
 
5.7. Advanced Special Class Methods
 
__cmp__ is called when you compare class instances. In general, you can compare any two Python objects, not just class instances, by using ==. There are rules that define when built-in datatypes are considered equal; for instance, dictionaries are equal when they have all the same keys and values, and strings are equal when they are the same length and contain the same sequence of characters. For class instances, you can define the __cmp__ method and code the comparison logic yourself, and then you can use == to compare instances of your class and Python will call your __cmp__ special method for you.
 
而在Python 2.5 Document中提及的是:
3.4.1 Basic customization


__lt__(self, other)
__le__(self, other)
__eq__(self, other)
__ne__(self, other)
__gt__(self, other)
__ge__(self, other)
New in version 2.1. These are the so-called ``rich comparison'' methods, and are called for comparison operators in preference to __cmp__() below. The correspondence between operator symbols and method names is as follows: x<y calls x.__lt__(y), x<=y calls x.__le__(y), x==y calls x.__eq__(y), x!=y and x<>y call x.__ne__(y), x>y calls x.__gt__(y), and x>=y calls x.__ge__(y).
A rich comparison method may return the singleton NotImplemented if it does not implement the operation for a given pair of arguments. By convention, False and True are returned for a successful comparison. However, these methods can return any value, so if the comparison operator is used in a Boolean context (e.g., in the condition of an if statement), Python will call bool() on the value to determine if the result is true or false.

There are no implied relationships among the comparison operators. The truth of x==y does not imply that x!=y is false. Accordingly, when defining __eq__(), one should also define __ne__() so that the operators will behave as expected.

There are no reflected (swapped-argument) versions of these methods (to be used when the left argument does not support the operation but the right argument does); rather, __lt__() and __gt__() are each other's reflection, __le__() and __ge__() are each other's reflection, and __eq__() and __ne__() are their own reflection.
Arguments to rich comparison methods are never coerced.
__cmp__(self, other)

Called by comparison operations if rich comparison (see above) is not defined. Should return a negative integer if self < other, zero if self == other, a positive integer if self > other. If no __cmp__(), __eq__() or __ne__() operation is defined, class instances are compared by object identity (``address''). See also the description of __hash__() for some important notes on creating objects which support custom comparison operations and are usable as dictionary keys. (Note: the restriction that exceptions are not propagated by __cmp__() has been removed since Python 1.5.)

dive into python中说:"你可以定义 __cmp__ 方法,自已编写比较逻辑,然后你可以使用 == 来比较你的类,Python 将会替你调用你的 __cmp__ 专用方法。"
而在Python 2.5 document里面是说:使用 == 来比较的时候,python会替你调用用 __eq__专用方法。而且貌似调用__cmp__的时候,当 xx == xx 为真的时候,__cmp__返回 零。
 
糊涂了...,不过感觉 Python 2.5 Document里面的解释比较正确。 

***************************************************

maillist里面的Tao Fei 给了个很好的例子:

__cmp__(        self, other)
Called by comparison operations if rich comparison (see above) is not defined.

应该是先找 __eq__ ,找不到在找 __cmp__
DiveIntoPython 的说法也许不够精确.
 不过也不能说它错.

>>> class A:
...         def __cmp__(self,other):
...                 print '__cmp__ is called'
...                 return 0
...
>>> a1 = A()
>>> a2 = A()
>>> a1 == a2
__cmp__ is called
True

>>> class A:
...         def __cmp__(self,other):
...                 print '__cmp__ is called'
...                 return 0
...         def __eq__(self,other):
...                 print '__eq__ is called'
...                 return True
...
>>> a1 = A()
>>> a2 = A()
>>> a1 == a2
__eq__ is called
True
>>>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值