[Python-Dev] Comparison and (user) confusion

François Pinard pinard at iro.umontreal.ca
Thu Jul 1 19:37:16 CEST 2004


Hi, people.

Yesterday, as a user, I made a little bug which I quickly resolved, but
over which I could have spent some time, having been younger to Python.

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. :-)

P.S. - Of course, replacing `__cmp__' by `__lt__' solved my problem.

P.P.S. - This is Python 2.3.3 on Linux, in case it matters.

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard



More information about the Python-Dev mailing list