Style question on recursive generators
Alex Martelli
aleaxit at yahoo.com
Tue Oct 19 04:58:11 EDT 2004
Dima Dorfman <d+pylist at nospamplease.trit.org> wrote:
> Alex Martelli <aleaxit at yahoo.com> wrote:
> > If the issue is strictly one of whether it's worth somehow making
> > for x in y: yield x
> > terser (by a new kw, combination of kw, and/or punctuation) I guess we
> > can discuss that (I just don't see the gain to offset the price of
> > introducing new syntax and one more language thing to learn for
> > Pythonistas and newcomers to Python).
>
> I find myself writing a lot of recursive generators, and a while ago I
> implemented this:
>
> yield *other
>
> to mean:
>
> for x in other:
> yield x
>
> No new keywords, and the asterisk is already used for a similar
> purpose (iterator expansion) in the function call syntax.
>
> Thoughts? "Neat!" or "Yuck!"?
Looks to me roughly equivalent to such proposals as 'yield from other';
not totally out of whack but IMHO not warranted. Re specifically
expanding the use of prefix *, I think there might be other good uses
for that -- say:
a = b, *c, d
meaning the same as
a = (b,) + tuple(c) + (d,)
or the old, currently out of favour proposal
a, b, c, *d = e
where d gets (an iterator for) 'the rest of e' after the first three
items of e are unpacked to a, b, and c. If your idea to specialcase the
* after a yield can work together with these other possibilities, the
whole package might perhaps get in together.
Alex
More information about the Python-list
mailing list