Greater and less than operators [was Re: [Tutor] beginning to code]
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Wed Sep 20 00:42:59 EDT 2017
On Tue, 19 Sep 2017 17:26:55 -0700, Rick Johnson wrote:
> Of course, allowing all objects to use the `==`, `!=` sugars makes
> perfect sense, but `<`, `>`, `<=`, `>=` are meaningless outside of
> numeric-ish types.
You've never wanted to sort strings? How do you sort strings unless you
have a concept of which string comes before the other, i.e. < operator?
>>> 'xyz' < 'abc'
False
Same applies to lists of items. Provided the items are compatible with
ordering, so are the lists. Likewise other sequences.
And for that matter, sets. Maybe we'd prefer to use the proper
mathematical operators ⊂ and ⊃ for subset and superset, but a good ASCII
equivalent would be < and > instead.
Likewise, any time you want to express some sort of order relationship:
pawn < rook < knight < bishop < queen < king
perhaps. (Although, in real games of chess, the value of a piece partly
depends on what other pieces are left on the board.)
Or perhaps you have some sort of custom DSL (Domain Specific Language)
where > and < make handy symbols for something completely unrelated to
ordering:
cake > oven # put cake into the oven
cake < oven # remove cake from oven
I don't mean that as a serious example of a useful DSL. But it is the
kind of thing we might want to do. Only hopefully less lame.
--
Steven D'Aprano
“You are deluded if you think software engineers who can't write
operating systems or applications without security holes, can write
virtualization layers without security holes.” —Theo de Raadt
More information about the Python-list
mailing list