recursion in __cmp__

Discussion in 'Python' started by Victor Safronovich, Nov 5, 2004.

  1. 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())

    <__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
     
    Victor Safronovich, Nov 5, 2004
    #1
    1. Advertising

  2. 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)


    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
     
    Alex Martelli, Nov 5, 2004
    #2
    1. Advertising

  3. Victor Safronovich

    John Doe Guest

    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: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())

    > <__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
     
    John Doe, Nov 5, 2004
    #3
  4. Victor Safronovich

    Peter Otten Guest

    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
     
    Peter Otten, Nov 5, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Fulton
    Replies:
    0
    Views:
    686
    Jim Fulton
    Jun 29, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    607
    Tim Peters
    Jun 29, 2003
  3. Gary Robinson

    bisect uses __cmp__()?

    Gary Robinson, Jul 29, 2003, in forum: Python
    Replies:
    0
    Views:
    353
    Gary Robinson
    Jul 29, 2003
  4. =?ISO-8859-1?Q?Jan-Erik_Meyer-L=FCtgens?=

    dictionary keys, __hash__, __cmp__

    =?ISO-8859-1?Q?Jan-Erik_Meyer-L=FCtgens?=, Nov 4, 2003, in forum: Python
    Replies:
    8
    Views:
    391
    Michael Hudson
    Nov 5, 2003
  5. Replies:
    8
    Views:
    798
    John Reye
    Apr 26, 2012
Loading...

Share This Page