True == 1 weirdness
steve at pearwood.info
Fri Sep 18 14:30:26 CEST 2015
On Fri, 18 Sep 2015 07:26 am, Random832 wrote:
> I don't even think chaining should
> work for all *actual* comparison operations.
I don't see why. Mathematicians chain comparisons all the time. If the
language implements the same semantics as mathematicians already use, why
do you dislike that?
I don't see the benefit in restricting the language to something less
expressive and more verbose than standard comparison chaining.
> Say you have this statement:
> (1) a < b = c <= d
> While it may *actually* mean this:
> (2) a < b and b = c and c <= d
> It *semantically* means this:
> (3) a < b and a < c and a < d and b = c and b <= d and c <= d
Only if the comparisons are transitive, which they may not be. If they are,
then something like this:
a < b < c
implies a < c too. But not all comparisons are transitive.
> The ones that are included logically imply the ones that are not, for
> any sane definition of these operators.
Transitivity is *not* required for sanity. Nontransitivity is a very useful
property for games, e.g. Rock-Paper-Scissors. It would be a very boring
game indeed if the relation
Rock < Paper < Scissors
(where < means "is beaten by") was transitive.
Intransitivity is likewise very important in consumer preferences,
psychology, and voting (voter preferences are often nontransitive, e.g.
voters prefer the Flog-em-and-hang-em Party over the Treehugger Party, the
Treehugger Party over the Raving Monster Loony Party, and the Raving
Monster Loony Party over the Flog-em-and-hang-em Party.
[Aside: some voting systems do guarantee transitivity, but only at the cost
of some other desirable property, such as no negative votes or no dictator.
Other voting systems make nontransitive elections unlikely.]
Other real-world examples include status hierarchies and pecking orders, and
nontransitive dice. (Google it, I'm too lazy to provide a link.)
> And if your operators *aren't*
> sane, it's better to be explicit about what you are doing.
Why? The results are perfectly well-defined however you write them.
Transitivity or not,
"Rock beats Scissors beats Paper beats Rock"
means the same thing as
"Rock beats Scissors, and Scissors beats Paper, and Paper beats Rock"
except it's much shorter.
More information about the Python-list