Tell me the truth
Erik Johnson
nobody at invalid.com
Thu Mar 8 12:10:59 EST 2007
"Duncan Booth" <duncan.booth at invalid.invalid> wrote in message
news:Xns98EDA7205C264duncanbooth at 127.0.0.1...
> francois.petitjean at bureauveritas.com wrote:
>
> > After much head scrating and experimenting with dis.dis() I have found
> > that chaining comparisons (with is or ==) a == b == c in Python is
> > never a good idea. It is interpreted as
> > ( a == b ) and ( b == c)
> > Such a magic is fine when we write:
> > if 0.0 <= x < 1.0:
> > but it renders the outcome of if a == b == c: somewhat confusing.
>
> I don't understand why it is confusing. What other meaning would you
> expect?
I can see how one might get themselves confused. I think it simply boils
down to thinking that
A == B == C is shorthand for: (A == B) == C
It's not. You're asserting that A is equivalent to B and B is equivalent to
C, NOT that the value of comparing A to B is equivalent to C:
>>> False is False
True
>>> False is False is True
False
If what you want is the second form, a pair of parenthesis forcing order of
evaluation will give it to you.
>>> (False is False) is True
True
-ej
More information about the Python-list
mailing list