Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #37519
| Date | 2013-01-23 17:29 -0600 |
|---|---|
| From | Tim Chase <python.list@tim.thechases.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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.932.1358983703.2939.python-list@python.org> (permalink) |
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 | 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