Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Sun Apr 6 19:48:22 EDT 2014
On Sun, 06 Apr 2014 23:10:47 +0300, Marko Rauhamaa wrote:
> Steven D'Aprano <steve+comp.lang.python at pearwood.info>:
>
>> 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!
All we know is that the name "len" will be looked up, it may or may not
find something, that thing may or may not be callable, calling it with a
list may or may not succeed, and it may or may not return 3 when given
that specific list as input. From the perspective of wanting to prove
things about the code, there's not a lot of certainty there.
If we replace Python with a Python-like language which is closer to the
traditional Algol mode of built-in functions being keywords (and hence
unable to be shadowed or deleted) then we can reason about the behaviour
more successfully. Alas, a Python-like language is not Python, and our
discussion is about whether or not *Python* is suitable for this use.
>>> * combinatory birds in forests
>>
>> I don't believe that came from academia. If I've understood correctly,
>> that was from a non-academic book on applying the lambda calculus to
>> solve practical applications.
>
> It is academic because the author, Raymond Smullyan, was a professor of
> philosophy and, more importantly, my professor selected that as a
> textbook for us graduate students.
Ah. Well they do that, don't they? I've always consider the ability of
professors to select their own book as text to be a classic case of
conflict of interest. They're supposed to pick the best book, not
necessarily the one that earns them money.
--
Steven D'Aprano
http://import-that.dreamwidth.org/
More information about the Python-list
mailing list