Loop-and-a-half (Re: Curious assignment behaviour)

Alex Martelli aleax at aleax.it
Sat Oct 13 04:59:38 EDT 2001


Huaiyu Zhu wrote:

>>while data.set(file.readline()) != 'end':
>>    process(data.get())
> 
> Alex, what's your opinion comparing these to the following?
> 
> while line = file.xreadlines(); line != 'end':
>     etcetc(line)
> 
> I'm pushing for this because it is not just syntactic suguar.  It gives

Your approach is indeed far more general -- I just showed somewhat-clunky 
syntax that has the advantage of working right now (and ever since the Dawn 
of Python) when one is transliterating code from other languages (I 
wouldn't use it in code meant to be idiomatically Pythonic, or Pythonically 
idiomatic).  However, the ability to have arbitrary statements as part of 
an "expression" may be TOO general for comfort -- right now I can rely on 
the fact that an expression can never rebind my local variables (and this 
for example makes eval usable where exec might not be), the generalization 
would take such guarantees away.  A much more wary look at the trade-offs 
is therefore surely warranted.


Alex




More information about the Python-list mailing list