[Python-ideas] PEP for issue2292, "Missing *-unpacking generalizations"
Joshua Landau
joshua.landau.ws at gmail.com
Mon Jul 15 13:08:14 CEST 2013
On 15 July 2013 11:40, Oscar Benjamin <oscar.j.benjamin at gmail.com> wrote:
> On 13 July 2013 01:25, Joshua Landau <joshua.landau.ws at gmail.com> wrote:
>> A blessing from the Gods has resulted in
>> http://www.python.org/dev/peps/pep-0448/! See what you think; it's not too
>> changed from before but it's mighty pretty now.
>
> I definitely like the general colour of this shed but would probably
> repaint one side of it: while unpacking can create tuples, sets, lists
> and dicts there's no way to create an iterator. I would like it if the
> unpacking syntax could somehow be used for iterators. For example:
>
> first_line = next(inputfile)
> # inspect first_line
> for line in chain([first_line], inputfile):
> # process line
>
> could be rewritten as
>
> first_line = next(inputfile):
> for line in first_line, *inputfile:
> pass
>
> without reading the whole file into memory.
>
> Using the tuple syntax is
> probably confusing but it would be great if there were some way to
> spell this and get an iterator instead of a concrete collection.
It's useful... I don't dislike it.
It might even be a good fit to use tuple syntax for this:
1) We already use the tuple's comprehension syntax for iterators
2) If you think of "*" as yield from, it's not too different from
changing a function to a generator
But you have the "unification" disadvantage, as well as the added
complexity of implementation.
Side note:
In fact, I'd much like it if there was an iterable "unpacking" method
for functions, too, so "chain.from_iterable()" could use the same
interface as "chain" (and str.format with str.format_map, etc.). I
feel we already have a good deal of redundancy due to this.
> Also this may be outside the scope of this PEP but since unpacking is
> likely to be overhauled I'd like to put forward a previous suggestion
> by Greg Ewing that there be a way to unpack some items from an
> iterator without consuming the whole thing e.g.:
>
> a, ... = iterable
That's definitely outside of this PEP's scope ;). Also, I think you
oversimplified your last version -- you still need a try-except
AFAICT.
More information about the Python-ideas
mailing list