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


Groups > comp.lang.python > #37519

Re: Arent these snippets equivalent?

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!cs.uu.nl!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
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; 'threads,': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; '"this': 0.13; 'cases': 0.15; "'xxx'": 0.16; '-tkc': 0.16; '0):': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'lambda': 0.16; 'message- id:@tim.thechases.com': 0.16; 'none"': 0.16; 'presume': 0.16; 'pythonic': 0.16; 'row': 0.16; 'roy': 0.16; 'subject:these': 0.16; 'syntactic': 0.16; 'true:': 0.16; 'wrote:': 0.17; 'expanded': 0.17; 'yield': 0.17; 'tests': 0.18; 'cc:2**0': 0.23; 'example': 0.23; 'this:': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'checking': 0.27; '(including': 0.30; '(and': 0.32; 'quickly': 0.32; 'could': 0.32; 'print': 0.32; 'cases,': 0.33; 'proposals': 0.33; 'skip:d 20': 0.34; 'subject:?': 0.35; 'list.': 0.35; 'really': 0.36; 'ability': 0.36; 'test': 0.36; 'subject:: ': 0.38; 'mean': 0.38; 'some': 0.38; 'things': 0.38; 'your': 0.60; 'easy': 0.60; 'most': 0.61; 'more': 0.63; 'customized': 0.64; 'smith': 0.71; 'saw': 0.75; 'felt': 0.75; 'attractive': 0.78; 'received:50.22': 0.84; 'received:108': 0.91; 'results,': 0.91
Date Wed, 23 Jan 2013 17:29:47 -0600
From Tim Chase <python.list@tim.thechases.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130107 Thunderbird/17.0.2
MIME-Version 1.0
To Roy Smith <roy@panix.com>
Subject Re: Arent these snippets equivalent?
References <d7191cec-d963-42c8-90ba-db6d1359ceeb@googlegroups.com> <mailman.923.1358979203.2939.python-list@python.org> <roy-3DE6E9.17475523012013@news.panix.com>
In-Reply-To <roy-3DE6E9.17475523012013@news.panix.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - boston.accountservergroup.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - tim.thechases.com
Cc python-list@python.org
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.932.1358983703.2939.python-list@python.org> (permalink)
Lines 59
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1358983703 news.xs4all.nl 6860 [2001:888:2000:d::a6]:46317
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:37519

Show key headers only | View raw


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.

-tkc


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