[Python-ideas] PEP for issue2292, "Missing *-unpacking generalizations"
Oscar Benjamin
oscar.j.benjamin at gmail.com
Mon Jul 15 12:40:45 CEST 2013
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.
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
could be roughly equivalent to:
try:
a = next(iter(iterable))
except StopIteration:
raise ValueError('Need more than 0 items to unpack')
I currently write code like:
def parsefile(inputfile):
inputfile = iter(inputfile)
try:
first_line = next(inputfile)
except StopIteration:
raise ValueError('Empty file')
# Inspect first_line
for line in chain([first_line], inputfile):
# Process line
But with the changes above I could do
def parsefile(inputfile):
inputfile = iter(inputfile)
first_line, ... = inputfile
# Inspect first_line
for line in first_line, *inputfile:
# Process line
Oscar
More information about the Python-ideas
mailing list