[Python-Dev] Comparison and (user) confusion

Aahz aahz at pythoncraft.com
Mon Jul 12 15:49:52 CEST 2004


On Thu, Jul 01, 2004, François Pinard wrote:
>
> The problem appeared when I sub-classed `list', and used a `__cmp__'
> method in my sub-type to effect a specialised sort.  The sort did not
> behave as expected, as the `list' type provides rich comparisons, and
> that `sort', finding `__lt__', merely ignored my own `__cmp__'.
> 
> I wonder if Python could not have helped better here, by diagnosing
> a possible comparison confusion while creating the sub-class.  I do
> not know much know Python internals, but if C slots happen to be used
> for comparison methods, it might not be so unreasonable to expect such
> confusion to be detected whenever those slots are filled.
> 
> I presume here, maybe wrongly, that it does not make good sense mixing
> rich and non-rich comparisons in the same class.  Just a thought. :-)

There is one good reason for implementing both __cmp__() and rich
comparisons: when comparing is expensive, it makes sense to call cmp()
directly if you need the full three-way compare to branch.  (Decimal is
a good example of this -- at least it was when I originally wrote it,
and it probably still has the same need now.)

While your question also makes sense, I'd say it's the province of
PyChecker rather than Python itself.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"Typing is cheap.  Thinking is expensive."  --Roy Smith, c.l.py


More information about the Python-Dev mailing list