while c = f.read(1)

Antoon Pardon apardon at forel.vub.ac.be
Mon Aug 22 15:39:05 CEST 2005


Op 2005-08-22, Steve Holden schreef <steve at holdenweb.com>:
> Greg McIntyre wrote:
>> John Machin wrote:
>> 
>>>>Is some way to make this code more compact and simple? It's a bit
>>>>spaghetti.
>>>
>>>Not at all, IMHO. This is a simple forward-branching exit from a loop in
>>>explicable circumstances (EOF). It is a common-enough idiom that doesn't
>>>detract from readability & understandability. Spaghetti is like a GOTO
>>>that jumps backwards into the middle of a loop for no discernable reason.
>> 
>> 
>> While that is true, I had high hopes that I could do this:
>> 
>>   while c = f.read(1): # ...
>> 
>> And relative to that, it is more complex. And although I am nit-picking
>> to try to simplify this code, I wanted to understand why Python works
>> in this way (even if that's just "historical reasons"), and check to
>> see if there was not some shorter more modern Pythonic alternative.
>> 
> Guido explicitly decided he wanted a clear differentiation between 
> expressions and statements, and that assignment was to be a statement. 
> The logic behind this design decision is documented in
>
> http://www.python.org/doc/faq/general.html#why-can-t-i-use-an-assignment-in-an-expression
>
> which may have already been mentioned in this thread.

I can't help but find this a very poor reason for this decision.
Sure it is a hard to find bug. but that is not because the
assignment is also an expression but because the assigment operator
looks so much like an equality comparator. ':=' was already in use
as an assignment is a number of languages and using it would
have handled the typo problem just as well while still allowing
assignments in expressions. This answer gives me the impression
the matter wasn't thought through thoroughly.

-- 
Antoon Pardon



More information about the Python-list mailing list