Rich Comparisons Gotcha

M.-A. Lemburg mal at egenix.com
Wed Dec 10 14:44:45 EST 2008


On 2008-12-10 20:01, Luis Zarrabeitia wrote:
> On Wednesday 10 December 2008 10:50:57 am M.-A. Lemburg wrote:
>> On 2008-12-10 16:40, Luis Zarrabeitia wrote:
>>> Quoting Rasmus Fogh <rhf22 at mole.bio.cam.ac.uk>:
>>>> Rhamphoryncus wrote:
>> Rich comparisons were added to Python at the request of the
>> Numeric (now numpy) developers and they have been part of Python
>> a Numeric for many many years.
>>
>> I don't think it's likely they'll change things back to the days
>> of Python 1.5.2 ;-)
> 
> Please define "rich comparisons" for me. It seems that I do not understand the 
> term - I was thinking it meant the ability to override the comparison 
> operators, and specially, the ability to override them independently.

That's one of the features, rich comparisons added. Another is
the ability to return arbitrary objects instead of just booleans
or integers:

http://www.python.org/dev/peps/pep-0207/

David was a Numeric developer at the time (among other things).

> Even in statically typed languages, when you override the equality 
> operator/function you can choose not to return a valid answer (raise an 
> exception). And it would break all the cases mentioned above (element in 
> list, etc). But that isn't the right thing to do. The language doesn't/can't 
> prohibit you from breaking the equality test, but that shouldn't be 
> considered a feature. (a==b).all() makes no sense. 

Perhaps not in your application, but it does make sense in other
numeric applications, e.g. ones that work on vectors or matrixes.

I'd suggest you simply wrap the comparison in a function and then
have that apply the necessary conversion to a boolean.

>>> Even the transition itself could be done without breaking much code...
>>> Make the == op return an object that wraps the array of bools (instead of
>>> the array itself), give it the any() and all() methods, and make
>>> __nonzero__/__bool__ equivalent to all().
>> That would cause a lot of confusion on its own, since such an
>> object wouldn't behave in the same way as say a regular Python
>> list (bool([0]) == True).
> 
> I'm certain that something could be worked out. A quick paragraph that took me 
> just a few minutes to type shouldn't be construed as a PEP that will solve 
> all the problems :D.

As always: the Devil is in the details :-)

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Dec 10 2008)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________
2008-12-02: Released mxODBC.Connect 1.0.0      http://python.egenix.com/

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/



More information about the Python-list mailing list