What does a list comprehension do

Marko Rauhamaa marko at pacujo.net
Thu Nov 26 08:56:41 EST 2015


Antoon Pardon <antoon.pardon at rece.vub.ac.be>:

> I don't understand. What I propose would be a minor change in
> how list comprehension works. I don't see how your example
> can be turned into a list comprehension.

The list comprehension is only a special case of the interaction between
closures and variables. If you dabble with list comprehensions and
lambdas, you'll need to make consistent changes in closure semantics.

That would lead to the trouble I mentioned. Closures need to have
dynamic access to the variables they refer to.

This whole issue is a consequence of Python's assignment semantics. If
Python didn't allow altering the values of variables, we wouldn't be
having this discussion.

BTW, all(!?) other languages from Java to Scheme share closure semantics
with Python so you would really be making a mess by changing Python.


Marko



More information about the Python-list mailing list