Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37516

Re: Arent these snippets equivalent?

Date 2013-01-23 17:17 -0600
From Evan Driscoll <driscoll@cs.wisc.edu>
Subject Re: Arent these snippets equivalent?
References <d7191cec-d963-42c8-90ba-db6d1359ceeb@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.929.1358983138.2939.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Arent these snippets equivalent? Coolgg <gauravj123@gmail.com> - 2013-01-23 13:56 -0800
  Re: Arent these snippets equivalent? John Gordon <gordon@panix.com> - 2013-01-23 22:06 +0000
  Re: Arent these snippets equivalent? Chris Angelico <rosuav@gmail.com> - 2013-01-24 09:13 +1100
    Re: Arent these snippets equivalent? Roy Smith <roy@panix.com> - 2013-01-23 17:47 -0500
      Re: Arent these snippets equivalent? Tim Chase <python.list@tim.thechases.com> - 2013-01-23 17:29 -0600
      Re: Arent these snippets equivalent? Chris Angelico <rosuav@gmail.com> - 2013-01-24 10:29 +1100
      Re: Arent these snippets equivalent? Terry Reedy <tjreedy@udel.edu> - 2013-01-23 21:38 -0500
        Re: Arent these snippets equivalent? Coolgg <gauravj123@gmail.com> - 2013-01-23 21:01 -0800
        Re: Arent these snippets equivalent? Coolgg <gauravj123@gmail.com> - 2013-01-23 21:01 -0800
  Re: Arent these snippets equivalent? Evan Driscoll <driscoll@cs.wisc.edu> - 2013-01-23 17:17 -0600

csiph-web