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


Groups > comp.lang.python > #37530

Re: Arent these snippets equivalent?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.134.4.91.MISMATCH!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'value,': 0.03; 'advocate': 0.07; 'assign': 0.07; 'function,': 0.07; 'tests,': 0.07; 'ugly': 0.07; 'val': 0.07; '"if': 0.09; 'cursor': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'separating': 0.09; 'terry': 0.09; 'threads,': 0.09; 'def': 0.10; '"this': 0.13; 'cases': 0.15; 'producing': 0.15; "'xxx'": 0.16; '0):': 0.16; 'agree.': 0.16; 'independent,': 0.16; 'iterators': 0.16; 'lambda': 0.16; 'loops': 0.16; 'none"': 0.16; 'presume': 0.16; 'pythonic': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'row': 0.16; 'roy': 0.16; 'subject:these': 0.16; 'syntactic': 0.16; 'true:': 0.16; 'wrote:': 0.17; 'expanded': 0.17; 'instance,': 0.17; 'items.': 0.17; 'yield': 0.17; 'jan': 0.18; 'tests': 0.18; 'tim': 0.18; 'beauty': 0.22; 'example': 0.23; 'this:': 0.23; 'allows': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'checking': 0.27; 'separate': 0.27; 'mix': 0.27; 'header:X-Complaints-To:1': 0.28; 'chase': 0.29; '(including': 0.30; '(and': 0.32; 'quickly': 0.32; 'could': 0.32; 'print': 0.32; 'cases,': 0.33; 'proposals': 0.33; 'to:addr:python- list': 0.33; 'skip:d 20': 0.34; 'pm,': 0.35; 'subject:?': 0.35; 'list.': 0.35; 'received:org': 0.36; 'really': 0.36; 'ability': 0.36; 'test': 0.36; 'does': 0.37; 'two': 0.37; 'quite': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'some': 0.38; 'things': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'easy': 0.60; 'most': 0.61; 'production': 0.63; 'details': 0.63; 'more': 0.63; 'customized': 0.64; 'matter.': 0.65; 'smith': 0.71; 'saw': 0.75; 'felt': 0.75; 'attractive': 0.78; 'received:fios.verizon.net': 0.84; 'results,': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Arent these snippets equivalent?
Date Wed, 23 Jan 2013 21:38:54 -0500
References <d7191cec-d963-42c8-90ba-db6d1359ceeb@googlegroups.com> <mailman.923.1358979203.2939.python-list@python.org> <roy-3DE6E9.17475523012013@news.panix.com> <5100726B.2050300@tim.thechases.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Thunderbird/17.0
In-Reply-To <5100726B.2050300@tim.thechases.com>
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.939.1358995175.2939.python-list@python.org> (permalink)
Lines 66
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1358995175 news.xs4all.nl 6863 [2001:888:2000:d::a6]:43085
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:37530

Show key headers only | View raw


On 1/23/2013 6:29 PM, Tim Chase wrote:
> On 01/23/13 16:47, Roy Smith wrote:
>> while getchar() as c:
>>     putchar(c)
>>
>> That would give people (including me) the use case they're after most of
>> the time (call a function, assign the return value, and test it).  It's
>> way less klunky than:
>>
>> while True:
>>     c = getchar()
>>     if c:
> # I presume you mean "if not c:" here.
>>        break
>>     putchar()
>
> I was a pretty strong advocate early in one of these long threads, and
> for the simple cases, it's some attractive syntactic sugar. However, I
> found that it quickly blossomed into a lot of really ugly edge cases
> (multiple tests, multiple results, checking for "is None" vs. false'ness
> or some other condition such as "< 0").  I found that it was pretty easy
> to create a generator-wrapper for this:
>
>    def getter(fn):
>      while True:
>        val = fn()
>        if not val: break
>        yield val
>
>    # DB example
>    cursor = conn.cursor()
>    for row in getter(lambda: cursor.fetchmany()):
>      do_something(row)
>
>    # your getchar example
>    for c in getter(getchar):
>      do_something_else(c)
>
> This allowed me to have both the readability and customized tests (and
> the ability to return multiple values).  It could be expanded with
>
>    def getter(fn, is_at_end=lambda v: not v):
>      while True:
>        val = fn()
>        if is_at_end(val): break
>        yield val
>
> which would even allow you to do things like
>
>    for line in getter(file("foo.txt"), lambda s: s.find("xxx") < 0):
>      print "This line has 'xxx' in it:"
>      print line
>
> and those felt a lot more pythonic than any of the proposals I saw on
> the list.

I agree. To me, the beauty of iterators and for loops is that they 
separate production of the items of a collection from the processing of 
the same items. The two processes are often quite independent, and 
separating them clearly allows us to mix and match. For instance, when 
summing numbers, the internal details of producing the numbers does not 
matter.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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