[Python-ideas] "While" suggestion

Arnaud Delobelle arnodel at googlemail.com
Thu Jul 3 21:09:00 CEST 2008

2008/7/3 Facundo Batista <facundobatista at gmail.com>:
> 2008/7/3 Stavros Korokithakis <stavros at korokithakis.net>:
>> while my_file.read(1024) as data:
>>   do_something(data)
> Python explicitly disallows inline assignment ("while
> a=myfile.read():") because of the error propensity when confusing it
> with the comparation ("=="). But here we're gaining the same
> advantage, without that risk.

For me, the main advantage of binding being provided by a statement
rather than an expression is not that it eliminates confusion with
test for equality.  The main advantage is that it prevents *sneaky*
(re)binding.  You can't bind a name from within an expression, it has
to be clearly advertised:

   foo = 'new value'

When I'm scanning code later I'm not going to miss these.  IMHO,
giving that up would be a mistake.


More information about the Python-ideas mailing list