Separating __cmp__ from < and > ?
Bjorn Pettersen
bjorn at roguewave.com
Mon Aug 14 22:46:51 EDT 2000
Tim Peters wrote:
>
> [Huaiyu Zhu]
> > In many situations it is conceptually possible to have a!=b without
> > either a<b or a>b. Mathematically, this is the case for objects
> > that live in a partially ordered space.
> > ...
>
> As you've discovered, since all comparisons in Python today funnel thru a
> single cmp function, you can't use the builtin operators to achieve this.
>
> You can define your own (e.g.) lt gt eq ne etc methods, but can't map them
> to infix < > == != etc.
>
> Allowing redefinition of individual comparison operators is one of the goals
> of David Ascher's "Rich Comparisons" proposal, although the *real* thrust of
> that was to allow NumPy to do stuff like return a matrix of boolean values
> when doing elementwise matrix compares.
>
> The PEP for Rich Comparisons is at
>
> http://python.sourceforge.net/peps/pep-0207.html
>
> although it's currently somewhat less than fully fleshed out <ahem>.
I ran into the same limitation when I wanted to create an abstraction
level on top of SQL, ie. I wanted to be able to say something like:
t = Table("mytable")
sel = Selector()
sel << t['col1'] # select mytable.col1
# from mytable
sel.where( t['col1'] > 2 ) # where mytable.col1 > 2
the functional alternative was just too verbose, so I shelved the
idea...
-- bjorn
More information about the Python-list
mailing list