code review

Thomas Jollans t at jollybox.de
Sat Jun 30 18:05:26 EDT 2012


On 06/30/2012 11:47 PM, Terry Reedy wrote:
> On 6/30/2012 5:35 PM, Thomas Jollans wrote:
>> On 06/30/2012 11:07 PM, Alain Ketterlin wrote:
>>> Thomas Jollans <t at jollybox.de> writes:
>>>
>>>>>>>> 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. To know what the order is (or whether there is an order!) one
>> has to consult the language reference (which shouldn't be necessary), or
>> make an educated guess, which would almost certainly be correct, but
>> we're supposed to refuse the temptation to guess, right?
> 
> 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. As soon as you read it as a ternary operator, the two
comparisons are logically simultaneous. Doing the left hand comparison
first is clearly the intuitive thing to do, but it's still, strictly
speaking, arbitrary. Intuitive, clearly defined, but arbitrary.

The ternary conditional operator is a different beast because its
sub-operators "if" and "else" aren't also binary operators, so it's
obvious that it's parsed differently.

> One really should learn that. No
> 'implementation defined' ambiguity.




More information about the Python-list mailing list