New (?) suggestion re: 'while x = f(): ...'
Chris Liechti
cliechti at gmx.net
Tue May 28 18:22:32 EDT 2002
Jeff Epler <jepler at unpythonic.net> wrote in
news:mailman.1022619227.16027.python-list at python.org:
> Why not abuse the 'for' statement, since it performs an assignment to a
> user-determined name?
>
> Instead of
> x = v
> if x:
> ...
> you can write
> for x in G(v):
> ...
i don't like that, it doesn't show the programmers intention...
it implies that there is a loop but there isn't one realy.
> and instead of
> while 1:
> x = f()
> if not x: break
> ...
> you can write
> for x in H(f):
> ...
this isn't that bad if you have a very good name for H. but are thre realy
that many usages? most often f is readline but now we can iterate over
files.
could be interesting in list comprehensions, though.
> Given suitable names for G() and H() (and I haven't thought of any yet)
> does anybody favor this over the "pythonic" syntax?
all the effort just to hide "if not x: break" in H... apart frame making
the loop slower due to a function call it doesn't increase readabilty for
me. the other problem is that its limited to a function and it won't be
realy useful until you have curry'ing at hand too.
> Personally, I think
> I'll stick to doing it in the old-fashioned way, but I wanted to share
> my idea with the world...
Puh, we're lucky <just kidding>. what i realy would like is to have some
functional stuff in a module and there maybe something like H would have
its place.
chris
> Jeff Epler
> jepler at unpythonic.net
>
> def G(v):
> if not v: return ()
> return (v,)
or: G = lambda v: (v,)*bool(v)
hey, maybe we could do some Python golfing despite some worries about the
clean syntax <wink>
> def H(f):
> while 1:
> v = f()
> if not v: break
> yield v
--
Chris <cliechti at gmx.net>
More information about the Python-list
mailing list