Arent these snippets equivalent?

Evan Driscoll driscoll at cs.wisc.edu
Thu Jan 24 00:17:45 CET 2013


On 01/23/2013 03:56 PM, Coolgg wrote:
> 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)
> 
> Heres the article that sparked this question:
> http://wordaligned.org/articles/pythons-lesser-known-loop-control

There is at least one potentially-critical difference: what happens if
there is a 'continue' statement in the "..." part. The upper loop will
set data again, while the lower one will not.

So if what you mean is "are they equivalent no matter what legal Python
code you put in the ...", no, they aren't.

Evan

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 554 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20130123/46a007b0/attachment.sig>


More information about the Python-list mailing list