True == 1 weirdness

Random832 random832 at fastmail.com
Wed Sep 16 13:36:52 EDT 2015


On Wed, Sep 16, 2015, at 13:24, Steven D'Aprano wrote:
> On Thu, 17 Sep 2015 12:03 am, Random832 wrote:
>
> if word in line in text:
>     print("word in line and line in text")
> 
> But I agree with Tim Chase: I wouldn't use it, even though it's legal.

I just had another thought on *why* the other cases make me so uneasy.

The reason this is reasonable for simple cases like a > b > c or a < b
<= c is that, in their normal meanings, these operations are transitive.
a > b and b > c implies a > c. a < b and b <= c implies a < c. This is
also a good reason for allowing "==" or "is" anywhere - because it's a
natural way to write a graph including an equivalence class: a < b == c
< d implies b < d, a < c, and a < d. So it's a natural way of writing
it. On the other hand, a in b in c doesn't imply a in c for several
common cases, and a > b < c doesn't imply anything about the
relationship between a and c, so it really shouldn't be a single
expression.

A chained comparison doesn't *actually* compare non-adjacent elements,
but with well-defined transitive relationships (or... semi-transitive?
is there a word for this? My point being that when you include some ==
or mix <= and < together, it still allows you to make some statements
about their relationships), you are essentially saying "a, b, c are
ordered". The generalization to mixing arbitrary operators loses this
aspect.



More information about the Python-list mailing list