"Duncan Booth" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> [EMAIL PROTECTED] 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


-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to