[Python-ideas] Fwd: Fwd: Fwd: unpacking generalisations for list comprehension

Sven R. Kunze srkunze at mail.de
Sun Oct 16 08:34:58 EDT 2016


On 16.10.2016 07:08, David Mertz wrote:
>
> In case it wasn't entirely clear, I strongly and vehemently opposed 
> this unnecessary new syntax. It is confusing, bug prone, and would be 
> difficult to teach.
>

As this discussion won't come to an end, I decided to consult my girlfriend.

I started with (btw. she learned basic Python to solve some math quizzes):
"""
Let's install a list in another one.

 >>> meine_liste
[2, 3, 4, 5]
 >>> ['a', meine_liste, 'b']
['a', [2, 3, 4, 5], 'b']

Maybe, we want to remove the brackets.

 >>> ['a', *meine_liste, 'b']
['a', 2, 3, 4, 5, 'b']

Now, the problem of the discussion is the following:

 >>> [(i,i,i) for i in range(4)]
[(0, 0, 0), (1, 1, 1), (2, 2, 2), (3, 3, 3)]

Let's remove these inner parentheses again.

 >>> [*(i,i,i) for i in range(4)]
   File "<stdin>", line 1
SyntaxError: iterable unpacking cannot be used in comprehension

Some guy wanted to remove that restriction.
"""

I said a teacher contributed to the discussion and he finds this too 
complicated and confusing and does not even teach * in list displays at 
all. Her reaction was hilarious:

"Whom does he teach? Children?"
Me: "What? No, everybody I think. Why?"
She: "It's easy enough to remember what the star does."


She also asked what would the alternative would look like. I wrote:
"""
 >>> from itertools import chain
 >>> list(chain.from_iterable((i,i,i) for i in range(4)))
[0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3]
"""
Her reaction was like: "That's supposed to be easy to remember? I find 
the star easier."

In the end, she also added: "Not everybody drives a car but they still 
exist."

Cheers,
Sven

PS: off to the weekend. She's already complaint that I should spend less 
time inside my mailbox and more with her. ;)


More information about the Python-ideas mailing list