> > Suppose I have several variables, e.g.: a, b, c, d, e, f, g.
> >
> > I would like to be able to see if they're all the same, I don't care
> > what the value is, as long as they're equal. If they're all equal to
> > 0, or to "spam", or to ["cleese", "idle", "gilliam"], as long as
> > they're the same.
>
> First, you can actually do multiple equality testing the way your
> mathematics teacher would:
>
> if a == b == c == d == e == f == g:
>   # do stuff
>
> (this grates against my instincts in some ways, since it breaks
> associativity (because it means 'a == b == c' is different from '(a ==
> b) == c'), but it's in the language, so I guess it's the Right Way).


Chaining up equalities like that does feel weird to me too, but I think
that's just because of my prior bad experiences with C.  *grin*

But Python's chained comparisons are especially nice when we're doing
explicit bounds checking with the other comparison operators:

    low <= value < high

rather than:

    (low <= value) and (value < high)

There are a few more details about chained comparisons here:

    http://www.python.org/doc/ref/comparisons.html

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to