Arent these snippets equivalent?

John Gordon gordon at panix.com
Wed Jan 23 23:06:24 CET 2013


In <d7191cec-d963-42c8-90ba-db6d1359ceeb at googlegroups.com> Coolgg <gauravj123 at gmail.com> writes:

> Is this:

> while True:
>     data = fp.read(4096)
>     if not data:
>         break
>     ...

> not equivalent to this:

> data = fp.read (4096)
> while data:
>     ...{handle the chunk here}
>     data = fp.read (4096)

It looks equivalent to me (in terms of control flow).

But in the second example the fp.read() statement is duplicated, which is
undesirable.  It would be all too easy for a maintenance programmer to go
into the code a year from now and change the first one but miss the second
one.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon at panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"




More information about the Python-list mailing list