Fwd: Fwd: Fwd: unpacking generalisations for list comprehension
On Sat, Oct 15, 2016 at 10:09 AM, Steven D'Aprano
Not everything is a function. What's your point?
As far as I can see, in *every* other use of sequence unpacking, *t is conceptually replaced by a comma-separated sequence of items from t. If the starred item is on the left-hand side of the = sign, we might call it "sequence packing" rather than unpacking, and it operates to collect unused items, just like *args does in function parameter lists.
You brush over the fact that *t is not limited to a replacement by a comma-separated sequence of items from t, but *t is actually a replacement by that comma-separated sequence of items from t INTO an external context. For func(*t) to work, all the elements of t are kind of "leaked externally" into the function argument list's context, and for {**{'a': 1, 'b': 2, ...}} the inner dictionary's items are kind of "leaked externally" into the outer's context. You can think of the */** operators as a promotion from append to extend, but another way to see this is as a promotion from yield to yield from. So if you want to instead of append items to a comprehension, as is done with [yield_me for yield_me in iterator], you can see this new piece as a means to [*yield_from_me for yield_from_me in iterator]. Therefore I think it's a bit confusing that yield needs a different keyword if these asterisk operators already have this intuitive promotion effect. Besides, [*thing for thing in iterable_of_iters if cond] has this cool potential for the existing any() and all() builtins for cond, where a decision can be made based on the composition of the in itself iterable thing. cheers! mar77i
participants (21)
-
Alexander Heger
-
Brendan Barnwell
-
Brett Cannon
-
Chris Angelico
-
Daniel Moisset
-
David Mertz
-
Ethan Furman
-
Greg Ewing
-
Ivan Levkivskyi
-
Mariatta Wijaya
-
Mark Lawrence
-
Martti Kühne
-
Ned Batchelder
-
Neil Girdhar
-
Nick Coghlan
-
Paul Moore
-
Random832
-
Rob Cliffe
-
Steven D'Aprano
-
Sven R. Kunze
-
אלעזר