Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)
Rustom Mody
rustompmody at gmail.com
Sat Mar 22 13:16:43 EDT 2014
On Saturday, March 22, 2014 2:39:56 PM UTC+5:30, Ian wrote:
> On Fri, Mar 21, 2014 at 8:06 PM, Rustom Mody wrote:
> > Two: A comprehension variable is not bound but reassigned across the
> > comprehension. This problem remains in python3 and causes weird behavior when
> > lambdas are put in a comprehension
> Because Python as a language only has the concept of assignment, not
> binding. I think it would be weird and confusing if variables worked
> this way in comprehensions and nowhere else.
Bizarre viewpoint!
When you do this:
> There is also the default argument trick:
> >>> fl = [lambda y, *, x=x: x+y for x in [1,2,3]]
> >>> [f(2) for f in fl]
> [3, 4, 5]
how is that not-a-binding solution?
More generally, insofar as variable-scopes can be made and exited, there
is binding. Its just that imperative languages have
- assignment wherein the shape of the environment is preserved but
its content is changed
- there are binding-constructs -- functions, methods, classes etc etc
-- which leave extant bindings intact but create/remove new ones.
Ok, functional languages have only the latter.
But only the former?? Beyond assembly language I dont know what
that would/could be...
More information about the Python-list
mailing list