473,583 Members | 2,785 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 1463
Victor Safronovich <vs**********@n aumen.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__(sel f,*other):
print*'%s|%s'*% (`self`,*`other `)
return*cmp(self ,*other)
a = A()
cmp(a,A())
Does it get clearer with the following reimplementatio n 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
4903
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 that objects which compare equal have the same hash value." 3. "If a class does not define a __cmp__() method it should not define a __hash__()...
3
2884
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
3895
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 in the os module got objectified properly (or at least with some semblance of OO propriety!) In the issues section:
1
2268
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 come up with anything similar) and if there is a workaround for it short of restructuring the data structures in question. The fundamental issue...
2
1976
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 | (key, value) pairs. | dict(seq) -> new dictionary initialized as if via:
39
2794
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 incomparable, they are not equal but neither is one less or greater than the other. So I thought that either cmp could return None in this case...
8
1669
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 of my class. The value used for the comparison in either case should be the value of the datetime attribute of the class: from datetime import...
10
3313
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. Basically my __hash__ function is the following: def __hash__(self): out_int = 0
17
2713
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): File "./parse", line 25, in <module> print(x < y) TypeError: unorderable types: IP() < IP()
0
7829
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8330
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8198
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6587
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5705
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5379
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3850
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1438
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1162
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.