Documentation, assignment in expression.

Tim Chase python.list at tim.thechases.com
Sun Mar 25 09:03:14 EDT 2012


On 03/25/12 07:18, Alexander Blinne wrote:
> I am not sure I understand your argument. The doc section states that
>> " [...] in Python you’re forced to write this:
>> while True:
> line = f.readline()
> if not line:
> break
> ... # do something with line".
>> That simply isn't true as one can simply write:
>> for line in f:
> 	#do stuff

I think the complaint was backed by a bad example. Perhaps a DB 
example works better. With assignment allowed in an evaluation, 
you'd be able to write
 while data = conn.fetchmany():
 for row in data:
 process(row)
whereas you have to write
 while True:
 data = conn.fetchmany()
 if not data: break
 for row in data:
 process(row)
Granted, this can be turned into an iterator with a yield, making 
the issue somewhat moot:
 def db_iter(conn, *args, **kwargs):
 while True:
 data = conn.fetchmany(rows, *args, **kwargs)
 if not data: break
 for row in data:
 yield row
 for row in db_iter(conn):
 proecss(row)
-tkc


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /