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
Sun Apr 6 21:54:19 EDT 2014


On Monday, April 7, 2014 6:15:47 AM UTC+5:30, Terry Reedy wrote:
> On 4/6/2014 7:48 PM, Steven D'Aprano wrote:
> > On Sun, 06 Apr 2014 23:10:47 +0300, Marko Rauhamaa wrote:
> >> Steven D'Aprano :
> >>> On Sun, 06 Apr 2014 12:05:16 +0300, Marko Rauhamaa wrote:
> >>>> Python, BTW, is perfectly suitable for computer science.
> >>> I don't think it is. Python is not a pure functional language, so it's
> >>> very difficult to prove anything about the code apart from running it.
> >> Many classic CS ideas are expressed in terms of an Algol-like language.
> >> Nothing would prevent you from framing those ideas in a Python-like
> >> (pseudo)language. The question is mostly whether you prefer begin/end,
> >> braces or indentation.
> > Okay, I made an error in stating that it's because Python is not a pure
> > functional language. It's because Python is so dynamic that it is very
> > difficult to prove anything about the code apart from running it. Take
> > this code-snippet of Python:
> > n = len([1, 2, 3])
> > What can we say about it? Almost nothing!

> One merely needs to stipulate that builtin names have not been rebound 
> to give the answer: n is bound to 3. In the absence of code or text 
> specifying otherwise, that is the reasonable default assumption and the 
> one that most makes when reading code.

> Restricting the usage of Python's flexibility does not make it another 
> language. It makes it the actual language that the vast majority of 
> programs are written in and that people assume when reading code.

Well what Steven is saying (I think!) amounts to pointing out a gap:
- the actual language that the vast majority of programs are written in 
- python as in the Cpython (say) implementation

To close this gap requires trying to do what Brett Canon tried and more
generally PyPy tries.

Some small rarely used features which humans can invoke with with a
wave-fo-hands when reasoning about programs, end up being a
show-stopper in an implementation.

Every language has such: Fortran remains better for scientific computing
than C (leave aside C++) because among other things alias analysis for
Fortran arrays is more straightforward.



More information about the Python-list mailing list