473,700 Members | 2,608 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 1467
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
4906
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__() operation either."
3
2886
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
3916
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
2275
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 rests with defining classes which override __cmp__ and __hash__ in order to be used as keys in...
2
1977
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
2803
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 or throw a specific exception. People writing a __cmp__ method could do the same.
8
1676
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 datetime class GeneralizedTime(object): def __init__(self, time=None):
10
3321
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
2721
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
8649
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9219
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8931
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7819
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5904
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4660
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3097
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 we have to send another system
2
2395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2032
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.