code review

Alain Ketterlin alain at dpt-info.u-strasbg.fr
Sun Jul 1 01:03:14 CEST 2012


Thomas Jollans <t at jollybox.de> writes:

> On 06/30/2012 11:47 PM, Terry Reedy wrote:

>>>>>>>>> def is_valid_password(password):
>>>>>>>>>      return mud.minpass <= len(password) <= mud.maxpass
>>>>
>>>>> Which of the two comparisons is done first anyway?
>>>>> "In the face of ambiguity, refuse the temptation to guess."
>>>>
>>>> There is no ambiguity. See the language reference:

>>> Of course it's technically clearly defined, but the syntax isn't
>>> explicit.

>> Python pretty consistently evaluates expressions and equal precedence
>> operators left to right. 
>
> Yes. My sole point, really, is that "normally", one would expect these
> two expressions to be equivalent:
>
> a < b < c
> (a < b) < c
>
> This is clearly not true. That's the inconsistency here with the rest of
> the language.

No, comparison operators are different from arithmetic operators in that
they always evaluate to a boolean. There are only rare cases where it
makes sense to compare comparisons.

> As soon as you read it as a ternary operator, the two comparisons are
> logically simultaneous.

There is no ternary operator, you can chain as many as you want, using
whatever operators:

  if a <= b < c > d >= e:
      ...

Once you view this as a conjonction of conditions, you find back the
semantics of "and": short-circuit, left to right evaluation. I find this
consistent.

-- Alain.



More information about the Python-list mailing list