rich comparisions and old-style classes

trying to come up with a more concise description of the rich comparision machinery for pyref.infogami.com, I stumbled upon an oddity that I cannot really explain: in the attached example below, why is the rich comparision machinery doing *four* attempts to use __eq__ in the old- class case? </F> $ more test.py class old: def __init__(self, name): self.name = name def __repr__(self): return self.name def __eq__(self, other): print "EQ", self, other return NotImplemented def __cmp__(self, other): print "CMP", self, other return 0 class new(object): def __init__(self, name): self.name = name def __repr__(self): return self.name def __eq__(self, other): print "EQ", self, other return NotImplemented def __cmp__(self, other): print "CMP", self, other return 0 a = old("A") b = old("B") print a == b a = new("A") b = new("B") print a == b $ python test.py EQ A B EQ B A EQ B A EQ A B CMP A B True EQ A B EQ B A CMP A B True

Hi Fredrik, On Sun, Apr 30, 2006 at 08:13:40AM +0200, Fredrik Lundh wrote:
trying to come up with a more concise description of the rich comparision machinery for pyref.infogami.com,
That's quite optimistic. It's a known dark area.
I stumbled upon an oddity that I cannot really explain:
I'm afraid the only way to understand this is to step through the C code. I'm sure there is a reason along the lines of "well, we tried this and that, now let's try this slightly different thing which might or might not result in the same methods being called again". I notice that you didn't try comparing an old-style instance with a new-style one :-) More pragmatically I'd suggest that you only describe the new-style behavior. Old-style classes have two levels of dispatching starting from the introduction of new-style classes in 2.2 and I'm sure that no docs apart from deep technical references should have to worry about that. A bientot, Armin.

Armin Rigo wrote:
Hi Fredrik,
On Sun, Apr 30, 2006 at 08:13:40AM +0200, Fredrik Lundh wrote:
trying to come up with a more concise description of the rich comparision machinery for pyref.infogami.com,
That's quite optimistic. It's a known dark area.
I stumbled upon an oddity that I cannot really explain:
I'm afraid the only way to understand this is to step through the C code. I'm sure there is a reason along the lines of "well, we tried this and that, now let's try this slightly different thing which might or might not result in the same methods being called again".
I notice that you didn't try comparing an old-style instance with a new-style one :-)
More pragmatically I'd suggest that you only describe the new-style behavior. Old-style classes have two levels of dispatching starting from the introduction of new-style classes in 2.2 and I'm sure that no docs apart from deep technical references should have to worry about that.
I thought a good rule of thumb (especially for a tutorial) was : Either define ``__cmp__`` *or* define the rich comparison operators. Doing both is a recipe for confusion. Michael Foord
A bientot,
Armin. _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/fuzzyman%40voidspace.org.u...
participants (3)
-
Armin Rigo
-
Fredrik Lundh
-
Michael Foord