473,513 Members | 4,753 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

recursion in __cmp__

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
Jul 18 '05 #1
3 1457
Victor Safronovich <vs**********@naumen.ru> 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
Jul 18 '05 #2
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


Jul 18 '05 #3
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

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
4901
by: Jan-Erik Meyer-Lütgens | last post by:
In the Python Language Reference, I found the following statements about using objects as dictionary keys: 1. "__hash__() should return a 32-bit integer." 2. "The only required property is...
3
2880
by: Lee Harr | last post by:
Let's say I have a class class A: def __init__(self, level): self.level = level and I want to put some of these in a list and sort the list by level a1 = A(1)
31
3869
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's about time that all of the file and directory stuff...
1
2256
by: Erik Max Francis | last post by:
I've come across a limitation in unpickling certain types of complex data structures which involve instances that override __hash__, and was wondering if it was known (basic searches didn't seem to...
2
1969
by: Alex | last post by:
Entering >>> help(dict) Help on class dict in module __builtin__: class dict(object) | dict() -> new empty dictionary. | dict(mapping) -> new dictionary initialized from a mapping object's...
39
2766
by: Antoon Pardon | last post by:
I was wondering how people would feel if the cmp function and the __cmp__ method would be a bit more generalised. The problem now is that the cmp protocol has no way to indicate two objects are...
8
1666
by: insyte | last post by:
I have a class that has, as an attribute, an instance of datetime.datetime(). I would like to be able to compare my class directly to instances of datetime.datetime in addition to other instances...
10
3306
by: elventear | last post by:
Hello everyone, I am runing into recursion limit problems. I have found that the culprit was related to the __hash__ function that I had assigned to the objects that were added to a set. ...
17
2695
by: Johannes Bauer | last post by:
Hello group, I'm porting some code of mine to Python 3. One class has the __cmp__ operator overloaded, but comparison doesn't seem to work anymore with that: Traceback (most recent call last):...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5079
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1585
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.