Arent these snippets equivalent?
python.list at tim.thechases.com
Thu Jan 24 00:29:47 CET 2013
On 01/23/13 16:47, Roy Smith wrote:
> while getchar() as 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.
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:
val = fn()
if not val: break
# DB example
cursor = conn.cursor()
for row in getter(lambda: cursor.fetchmany()):
# your getchar example
for c in getter(getchar):
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):
val = fn()
if is_at_end(val): break
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:"
and those felt a lot more pythonic than any of the proposals I saw
on the list.
More information about the Python-list