Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #37509
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsreader4.netcologne.de!news.netcologne.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'compiler': 0.05; 'lines,': 0.05; 'chunk': 0.07; 'data:': 0.07; 'granted,': 0.07; 'lines.': 0.07; 'python': 0.09; '__future__': 0.09; 'braces': 0.09; '"from': 0.16; "'break'": 0.16; '(data': 0.16; '24,': 0.16; 'clear.': 0.16; 'expression,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'personally,': 0.16; 'ported': 0.16; 'subject:these': 0.16; 'true:': 0.16; 'wrote:': 0.17; "shouldn't": 0.17; 'thu,': 0.17; 'jan': 0.18; 'equivalent': 0.20; 'import': 0.21; 'do.': 0.21; 'assignment': 0.22; 'assuming': 0.22; "i'd": 0.22; "python's": 0.23; 'this:': 0.23; 'second': 0.24; 'header': 0.24; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'possible,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'lines': 0.28; 'received:209.85.212': 0.28; 'loop,': 0.29; 'unlikely': 0.29; 'function': 0.30; 'defining': 0.33; 'proposals': 0.33; 'to:addr :python-list': 0.33; 'received:google.com': 0.34; 'done': 0.34; 'clear': 0.35; 'doing': 0.35; 'too.': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'something': 0.35; 'there': 0.35; 'really': 0.36; 'but': 0.36; 'programmers': 0.36; 'method': 0.36; 'should': 0.36; 'does': 0.37; 'rather': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'your': 0.60; 'bottom': 0.60; 'most': 0.61; 'first': 0.61; 'repeat': 0.62; 'ever': 0.63; 'here': 0.65; 'goal': 0.74; "'while'": 0.84; '2013': 0.84; 'compare:': 0.84; 'lays': 0.84; 'thing,': 0.84; 'imagine': 0.96 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=kXyaW/pSB1JNDgFvV9gkQS0cj5x6dpFUBKVsP5Cysso=; b=AFll2o0+4CGJPeJurY3JnQ46Kk5jhn/9i0EyBfcmkAotyq6LK6jXEChrIolUPQ6v9i 8Wh5FeXCqrtQZQvLoY02gAfWf5oqCbI5MbH2dka6fVKQO5AunjBmPVHXqae2OQGxqHEh v0duzbamEyVmxdaNNYWE9jHgBnWMyiEFmlO5ra/SyEqg2anfWOZQarfKknHy0vL9xeJh oX23BVH/10eI7/LTRMBc0kSij3Uw56O+tSPzEetXLGuj1p8Ububo2rGKXxU1XvGST/07 oTo5I2XCrfgak1zQ5JwqVns+43WHtgIP3IovQposMhDycnmkZIYPsYsgDNZWiwKd6X+C KJgg== |
| MIME-Version | 1.0 |
| X-Received | by 10.52.178.225 with SMTP id db1mr2915392vdc.10.1358979194557; Wed, 23 Jan 2013 14:13:14 -0800 (PST) |
| In-Reply-To | <d7191cec-d963-42c8-90ba-db6d1359ceeb@googlegroups.com> |
| References | <d7191cec-d963-42c8-90ba-db6d1359ceeb@googlegroups.com> |
| Date | Thu, 24 Jan 2013 09:13:14 +1100 |
| Subject | Re: Arent these snippets equivalent? |
| From | Chris Angelico <rosuav@gmail.com> |
| To | python-list@python.org |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.923.1358979203.2939.python-list@python.org> (permalink) |
| Lines | 57 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1358979204 news.xs4all.nl 6946 [2001:888:2000:d::a6]:37747 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:37509 |
Show key headers only | View raw
On Thu, Jan 24, 2013 at 8:56 AM, Coolgg <gauravj123@gmail.com> 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)
They should do the same thing, but there's one critical difference in
the second: Edits to something that's really part of the loop now have
to be done twice, at the bottom of the loop and *before the loop*.
It's a violation of the principle Don't Repeat Yourself.
Personally, I'd much rather have a 'while' condition that does
assignment, but that's something Python's unlikely ever to do.
There've been various proposals to make that possible, but ultimately
the only way to make that work is for assignment to be an expression,
which is right up there alongside braces defining blocks.
(Wonder when we'll see "from __future__ import assignment_expression"
implemented...)
The 'break' method is the most common. Assuming you're doing something
as simple as the above, with a single function call and a clear
condition, it's pretty readable. Compare:
while (data = fp.read(4096))
{
... imagine about 20 lines here
}
and
while True:
data = fp.read(4096)
if not data: break
... imagine those same 20 lines, ported to Python
The critical parts of your while loop are in those first three lines.
It's the same goal as a C-style for loop - you can see everything you
need right up there at the loop header. All you have to do is
understand that the "loop header" is three lines long now.
With the second form of the loop, though, the loop header is down at
the bottom of the loop too. It's less clear. Granted, this might be
how a compiler lays it out in memory, but programmers shouldn't have
to read it that way.
ChrisA
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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