Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > recursion in __cmp__

Reply
Thread Tools

recursion in __cmp__

 
 
Victor Safronovich
Guest
Posts: n/a
 
      11-05-2004
please comment this
Python 2.2.3 (#42, May 30 2003, 18:12:0 [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())

<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
<__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
0
>>> cmp(a,5)

<__main__.A instance at 0x00989838>|5
<__main__.A instance at 0x00989838>|5
<__main__.A instance at 0x00989838>|5
<__main__.A instance at 0x00989838>|5
<__main__.A instance at 0x00989838>|5
<__main__.A instance at 0x00989838>|5
<__main__.A instance at 0x00989838>|5
<__main__.A instance at 0x00989838>|5
<__main__.A instance at 0x00989838>|5
<__main__.A instance at 0x00989838>|5
<__main__.A instance at 0x00989838>|5
<__main__.A instance at 0x00989838>|5
<__main__.A instance at 0x00989838>|5
<__main__.A instance at 0x00989838>|5
<__main__.A instance at 0x00989838>|5
<__main__.A instance at 0x00989838>|5
<__main__.A instance at 0x00989838>|5
<__main__.A instance at 0x00989838>|5
<__main__.A instance at 0x00989838>|5
<__main__.A instance at 0x00989838>|5
<__main__.A instance at 0x00989838>|5
0
 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      11-05-2004
Victor Safronovich <(E-Mail Removed)> wrote:

> please comment this
> Python 2.2.3 (#42, May 30 2003, 18:12:0 [MSC 32 bit (Intel)] on win32
> >>> class A:

> def __cmp__(self, other):
> print '%s|%s' %(`self`, `other`)
> return cmp(self, other)


What is there to comment? This is obviously and blatantly recursive,
since cmp(a, b) delegates back to a.__cmp__(b) [[for classic classes;
for a newstyle class it would be type(a).__cmp__(a, b) with the same
recursive effect in this case]].

Just like the simpler example: def __len__(self): return len(self)


Alex
 
Reply With Quote
 
 
 
 
John Doe
Guest
Posts: n/a
 
      11-05-2004
Really Victor,
no one comments on old versions. Try it on 2.3.

Dan

On Fri, 05 Nov 2004 00:45:28 -0800, Victor Safronovich wrote:

> please comment this
> Python 2.2.3 (#42, May 30 2003, 18:12:0 [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())

> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> <__main__.A instance at 0x00989838>|<__main__.A instance at 0x0097D120>
> 0
>>>> cmp(a,5)

> <__main__.A instance at 0x00989838>|5
> <__main__.A instance at 0x00989838>|5
> <__main__.A instance at 0x00989838>|5
> <__main__.A instance at 0x00989838>|5
> <__main__.A instance at 0x00989838>|5
> <__main__.A instance at 0x00989838>|5
> <__main__.A instance at 0x00989838>|5
> <__main__.A instance at 0x00989838>|5
> <__main__.A instance at 0x00989838>|5
> <__main__.A instance at 0x00989838>|5
> <__main__.A instance at 0x00989838>|5
> <__main__.A instance at 0x00989838>|5
> <__main__.A instance at 0x00989838>|5
> <__main__.A instance at 0x00989838>|5
> <__main__.A instance at 0x00989838>|5
> <__main__.A instance at 0x00989838>|5
> <__main__.A instance at 0x00989838>|5
> <__main__.A instance at 0x00989838>|5
> <__main__.A instance at 0x00989838>|5
> <__main__.A instance at 0x00989838>|5
> <__main__.A instance at 0x00989838>|5
> 0


 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      11-05-2004
Victor Safronovich wrote:

> please comment this
> Python 2.2.3 (#42, May 30 2003, 18:12:0 [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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
va_arg... recursion: changing arguments and the using recursion jononanon@googlemail.com C Programming 8 04-26-2012 08:37 PM
dictionary keys, __hash__, __cmp__ =?ISO-8859-1?Q?Jan-Erik_Meyer-L=FCtgens?= Python 8 11-05-2003 05:46 PM
bisect uses __cmp__()? Gary Robinson Python 0 07-29-2003 08:05 PM
RE: ExtensionClass/Persistent and __cmp__ is tricky Tim Peters Python 0 06-29-2003 04:49 PM
Re: ExtensionClass/Persistent and __cmp__ is tricky Jim Fulton Python 0 06-29-2003 12:56 PM



Advertisments