Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'separating': 0.09; 'terry': 0.09; 'threads,': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; '"this': 0.13; 'cases': 0.15; 'producing': 0.15; "'xxx'": 0.16; '0):': 0.16; 'agree.': 0.16; 'equivalence': 0.16; 'independent,': 0.16; 'iterators': 0.16; 'lambda': 0.16; 'loops': 0.16; 'none"': 0.16; 'presume': 0.16; 'pythonic': 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; 'wednesday,': 0.22; 'cc:2**0': 0.23; 'example': 0.23; 'needed.': 0.23; 'this:': 0.23; 'cc:no real name:2**0': 0.24; 'allows': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'checking': 0.27; 'separate': 0.27; 'mix': 0.27; 'chase': 0.29; '(including': 0.30; '(and': 0.32; 'quickly': 0.32; 'could': 0.32; 'print': 0.32; 'cases,': 0.33; 'curious': 0.33; 'everyone.': 0.33; 'proposals': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'thanks': 0.34; 'pm,': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'list.': 0.35; 'really': 0.36; 'ability': 0.36; 'functional': 0.36; 'test': 0.36; 'does': 0.37; 'two': 0.37; 'quite': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'some': 0.38; 'things': 0.38; 'received:209.85.214': 0.39; '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; '2013': 0.84; 'perspectives': 0.84; 'results,': 0.91 X-Received: by 10.50.217.201 with SMTP id pa9mr140735igc.17.1359003697733; Wed, 23 Jan 2013 21:01:37 -0800 (PST) Newsgroups: comp.lang.python Date: Wed, 23 Jan 2013 21:01:37 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=2001:4898:e0:1012:9c3f:d3f2:6fb4:55fa; posting-account=VLc5RAoAAAC4C_xI26SZy4CdrkVKO4_z References: <5100726B.2050300@tim.thechases.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 2001:4898:e0:1012:9c3f:d3f2:6fb4:55fa MIME-Version: 1.0 Subject: Re: Arent these snippets equivalent? From: Coolgg To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 132 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1359006419 news.xs4all.nl 6977 [2001:888:2000:d::a6]:55024 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37541 On Wednesday, January 23, 2013 6:38:54 PM UTC-8, Terry Reedy wrote: > 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 Thanks for all the perspectives everyone. I was just curious about the functional equivalence and I got what I needed.