[Python-3000] PEP 3132: Extended Iterable Unpacking

Steven Bethard steven.bethard at gmail.com
Thu May 3 18:24:46 CEST 2007


On 5/3/07, Georg Brandl <g.brandl at gmx.net> wrote:
> Steven Bethard schrieb:
> > On 5/3/07, Simon Percivall <percivall at gmail.com> wrote:
> >> On 2 maj 2007, at 20.08, Guido van Rossum wrote:
> >> > [Georg]
> >> >>>>>>> a, *b, c = range(5)
> >> >>>>>>> a
> >> >>>>     0
> >> >>>>>>> c
> >> >>>>     4
> >> >>>>>>> b
> >> >>>>     [1, 2, 3]
[snip]
> > In argument lists, *args exhausts iterators, converting them to
> > tuples. I think it would be confusing if *args in tuple unpacking
> > didn't do the same thing.
> >
> > This brings up the question of why the patch produces lists, not
> > tuples. What's the reasoning behind that?
>
> IMO, it's likely that you would like to further process the resulting
> sequence, including modifying it.

Well if that's what you're aiming at, then I'd expect it to be more
useful to have the unpacking generate not lists, but the same type you
started with, e.g. if I started with a string, I probably want to
continue using strings::

    >>> first, *rest = 'abcdef'
    >>> assert first == 'a', rest == 'bcdef'

By that same logic, if I started with iterators, I probably want to
continue using iterators, e.g.::

    >>> f = open(...)
    >>> first_line, *remaining_lines = f

So I guess it seems pretty arbitrary to me to assume that a list is
what people want to be using. And if we're going to be arbitrary, I
don't see why we shouldn't be arbitrary in the same way as function
arguments so that we only need on explanation.

STeVe
-- 
I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
        --- Bucky Katt, Get Fuzzy


More information about the Python-3000 mailing list