
As to the validity or legality of this code, it's both, and working as intended. A list comprehension of the form [STUFF for VAR1 in SEQ1 for VAR2 in SEQ2 for VAR3 in SEQ3] should be seen (informally) as for VAR1 in SEQ1: for VAR2 in SEQ2: for VAR3 in SEQ3: "put STUFF in the result" (If there are `if COND` phrases too those get inserted into the nested set of blocks where they occur in the sequence.) On Fri, Feb 23, 2018 at 9:41 AM, Chris Barker - NOAA Federal < chris.barker@noaa.gov> wrote:
Is it similar enough to
def f(x=[0]):
No, not at all — it’s a very different use case.
When I first saw this on the original thread, I needed to stare at it a good while, and then whip up some code to experiment with it to know what it did.
And not because I don’t know what a single element list means, or what it means to iterate over a single element list, or what two fors mean in a comprehension.
I was confused by the ‘x’ in the second iterable. I guess I’m (still) not really clear on the scope(s) inside a comprehension, and when the elements get evaluated in a list.
I expected that the list would be created once, with the value x had initially, rather than getting the-evaluated each time through the outer loop.
So I think that it is a very confusing use of comprehensions, and always will be. I’m still surprised it’s legal. Anyone know if this being allowed was deliberate or just kind of fell out of the implementation?
So no, I don’t think it should be promoted as idiomatic.
All that being said, it’s valid Python, so why not optimize it?
-CHB _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/ guido%40python.org
-- --Guido van Rossum (python.org/~guido)