[Python-Dev] (no subject)
Neil Girdhar
mistersheik at gmail.com
Tue Feb 10 07:49:36 CET 2015
On Tue, Feb 10, 2015 at 1:31 AM, Donald Stufft <donald at stufft.io> wrote:
>
> > On Feb 10, 2015, at 12:55 AM, Greg Ewing <greg.ewing at canterbury.ac.nz>
> wrote:
> >
> > Donald Stufft wrote:
> >> However [*item for item in ranges] is mapped more to something like
> this:
> >> result = []
> >> for item in iterable:
> >> result.extend(*item)
> >
> > Actually it would be
> >
> > result.extend(item)
> >
> > But if that bothers you, you could consider the expansion
> > to be
> >
> > result = []
> > for item in iterable:
> > for item1 in item:
> > result.append(item)
> >
> > In other words, the * is shorthand for an extra level
> > of looping.
> >
> >> and it acts differently than if you just did *item outside of a list
> comprehension.
> >
> > Not sure what you mean by that. It seems closely
> > analogous to the use of * in a function call to
> > me.
> >
>
> Putting aside the proposed syntax the current two statements are currently
> true:
>
> 1. The statement *item is roughly the same thing as (item[0], item[1],
> item[n])
> 2. The statement [something for thing in iterable] is roughly the same as:
> result = []
> for thing in iterable:
> result.append(something)
> This is a single loop where an expression is ran for each iteration of
> the
> loop, and the return result of that expression is appended to the
> result.
>
> If you combine these two things, the "something" in #2 becuase *item, and
> since
> *item is roughly the same thing as (item[0], item[1], item[n]) what you end
> up with is something that *should* behave like:
>
> result = []
> for thing in iterable:
> result.append((thing[0], thing[1], thing[n]))
>
That is what [list(something) for thing in iterable] does.
The iterable unpacking rule might have been better explained as follows:
————
On the left of assignment * is packing, e.g.
a, b, *cs = iterable
On the right of an assignment, * is an unpacking, e.g.
xs = a, b, *cs
————
In either case, the elements of "cs" are treated the same as a and b.
Do you agree that [*x for x in [as, bs, cs]] === [*as, *bs, *cs] ?
Then the elements of *as are unpacked into the list, the same way that
those elements are currently unpacked in a regular function call
f(*as) === f(as[0], as[1], ...)
Similarly,
[*as, *bs, *cs] === [as[0], as[1], …, bs[0], bs[1], …, cs[0], cs[1], …]
The rule for function calls is analogous:
————
In a function definition, * is a packing, collecting extra positional
argument in a list. E.g.,
def f(*args):
In a function call, * is an unpacking, expanding an iterable to populate
positional arguments. E.g.,
f(*args)
—
PEP 448 proposes having arbitrary numbers of unpackings in arbitrary
positions.
I will be updating the PEP this week if I can find the time.
>
> Or to put it another way:
>
> >>> [*item for item in [[1, 2, 3], [4, 5, 6]]
> [(1, 2, 3), (4, 5, 6)]
>
>
> Is a lot more consistent with what *thing and list comprehensions already
> mean
> in Python than for the answer to be [1, 2, 3, 4, 5, 6].
> ---
> Donald Stufft
> PGP: 7C6B 7C5D 5E2B 6356 A926 F04F 6E3C BCE9 3372 DCFA
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> https://mail.python.org/mailman/options/python-dev/mistersheik%40gmail.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150210/9b645b7e/attachment.html>
More information about the Python-Dev
mailing list