recursion in __cmp__
Peter Otten
__peter__ at web.de
Fri Nov 5 04:55:12 EST 2004
Victor Safronovich wrote:
> please comment this
> Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32
>>>> class A:
> def __cmp__(self, other):
> print '%s|%s' %(`self`, `other`)
> return cmp(self, other)
>
>>>> a = A()
>>>> cmp(a,A())
Does it get clearer with the following reimplementation of cmp()?
>>> class A:
... def __cmp__(self, other):
... return mycompare(self, other)
...
>>> def mycompare(a, b):
... try:
... return a.__cmp__(b)
... except AttributeError:
... return mycompare(id(a), id(b))
...
>>> mycompare(A(), A())
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in mycompare
File "<stdin>", line 3, in __cmp__
File "<stdin>", line 3, in mycompare
File "<stdin>", line 3, in __cmp__
[snip]
File "<stdin>", line 3, in __cmp__
File "<stdin>", line 3, in mycompare
RuntimeError: maximum recursion depth exceeded
>>>
Peter
More information about the Python-list
mailing list