Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

Mark H Harris harrismh777 at gmail.com
Mon Mar 24 15:47:11 EDT 2014


On 3/24/14 4:49 AM, Steven D'Aprano wrote:
> There's no doubt that lambda is less-often useful than is the def
> statement. But not only is it still useful, but there is a continual
> stream of people asking for Python to make it *more useful* by allowing
> the body of a lambda to be a full block, not just a single statement.
> Guido has stated (although I cannot remember where, so cannot paste a
> link) that he accepts that in principle, so long as somebody comes up
> with acceptable syntax.

    Now, this will seem strange to you, but if lambda were expanded to 
include an entire block, well now, I would know two things:
   1) python has commitment to lambda    and
   2) lambda would become WAY more useful... ie., the only way to have a 
"block expression" embedded in an expression !  I'd vote for that. But 
as someone else noted, inner functions work as well (now) and are really 
more readable, so ...

> It's used in calendar, cgitb, configparser, difflib, decimal, functools,
> inspect, os, pydoc and others. Python is a programming language aimed at
> programmers, not a toy. It should include tools that programmers use,
> like threads, closures, first-class functions. And lambda.

    This is a valid point (its used a couple of hundred times). It could 
be replaced in probably of week of constant programming effort.  Neither 
here nor there, because nobody at this point is demanding it.

>> Lambda is a problem of confusion for scientists and other
>> mathematicians (amateur and otherwise) {snip}
>{snip}
> But nor should the confusion of somebody
> who doesn't know the language count for much.

    This is the main disagreement between the two of us on this topic. 
It should count for TONS; whole metric TONs. Because someone who 
understands python primarily, and after five years of coding (say me) 
somebody tells (me) to use lambda in a key clause so that my sort will 
work, or work better, or work "their" way; and I have no idea why I need 
this nameless function (after I find out that "that" is what it is!) 
Its confusing, and please contrast between complicated. I don't mind 
complicated, or extensive, or even hard, but when something is 
needlessly confusing, then why?

> Python's ints don't work exactly the same as ints do in some other
> languages, nor do Python's strings, nor is Python's object model
> precisely the same as that of some other languages. I think that it would
> be a ridiculous idea to discard int, str and the entire object system
> just because some people are surprised that Python doesn't behave exactly
> like some other language. I don't think lambda is any different.

    Your logic is faulty here.  Conceptually python's lambda doesn't 
work as expected, from contexts that define lambda and where lambda in 
functional programming is VERY important. For one thing, why would we 
want to "spell out" the word "lambda".    ( \x y -> x + y ) a b )
If we're going to use lambda, then use it.


> Other languages -- not even Lisp -- don't get to be the sole decider as
> to what counts as an anonymous function. Python's lambda may be limited,
> but it is a perfectly fine lambda for what it is intended to do.

    Is that the same as George W. Bush's "decider" ?

    I agree, and so does the link I referenced, for what it was designed 
to do, its powerful; esp if one needs to embed a function in an 
expression;  that is what lambda is for.

>> Its also confusing to sophisticated users of all
>> stripes who may not be aware of "lambda" at all.
>
> If they're not aware of lambda, how are they confused by it?

    See above.  They are recommended its use (maybe on this list) and 
they just don't get it; because its unnecessarily confusing.

> No, not really. I don't understand who your intended audience for Python
> is. If I were to try to guess, I suspect that you want to dumb Python
> down until it is like Dartmouth BASIC circa 1980 except without the line
> numbers and with a slightly different syntax. Perhaps I'm wrong, I
> certainly hope I'm wrong, but that's the impression I got from your
> comments on the python-ideas list.

    Actually it was circa 1964. Actually, it was precisely 1964 (I was 
there).

    No, I'm not advocating for dumb-down simplicity. Aristotle said that 
virtue is found at the mean. Either end of the spectrum is a problem 
(overly complicated, towards overly simplified) those end-points we must 
flee like the black plague. Normal people with reasonable training 
(college education) should be able to use python easily without 
confusion and without a computer science degree. Programming should be a 
liberal art available to everyone in the set of all "normal" educated 
people. Some developers (themselves perhaps with degrees Ph.D. in 
mathematics or computer science) forget about the fact that "normal" 
educated people want to leverage their computer (maybe with python) for 
problem solution without having to become a professional computer 
scientist. I am advocating for those people.

> You want to remove lambda because it causes confusion. Okay, for the sake
> of the argument I will agree that lambda is confusing. Do you know what
> else is confusing?

    I don't want to remove anything at this point. I am only suggesting 
that over time the python community might evaluate whether the lambda 
(map, filter, reduce) thing is a language benefit. If so, manet in 
aeternum.  Elif, deprecate it and over time phase it out completely. Its 
a community decision of course, and I'm NOT demanding anything.

> Threading is confusing. So is multiprocessing. So are databases. Unicode
> is confusing. First-class functions are confusing. Recursion is
> confusing. Closures are confusing. List comprehensions are confusing.
> What the hell are trampolines? I'm certainly confused by them.

    Argument by analogy almost never works. Please don't confuse my word 
"confusing" with your interpretation "complicated". Actually, I do not 
find any of those above mentioned as "confusing," while I do admit they 
are complicated--even extremely complicated. An intelligent person 
educated in the liberal arts should be able to sit down over time and 
pick-up the concepts in a new area--without--being confused by the 
material or presentation. Lambda is confusing NOT because its 
complicated (because its not) but because 1) it does not work like other 
lambda concepts in functional programming, and 2) because the syntax 
does not lend itself easily to immediate interpretation (by normal 
people) nor does it lend itself easily to explanation even in the best 
effort.

> If we remove lambda because it is confusing, shouldn't we also remove all
> these other confusing things? Where shall we stop?

    No.   see above.

> Should we stop only when Python is a confusion-free, utterly useless toy
> language? I don't think so. Or should we stop *before* going down that
> path? You want to single out lambda. I don't think we should.

    No.   see above.

> I didn't say that Python can be used as a functional language. I said you
> can write functional code in Python. And that is so obviously true that I
> cannot believe that you are disputing it.

    I'm not disputing that, not in the least. But advertising that 
python can be used to write functional code is misleading (particularly 
when lambda is included ;map,filter, reduce) because those who KNOW 
functional programming are going to be either 1) confused, or more 
likely, 2) disappointed.  Yes, I can make a nameless function 
(expression) and pass it to another function as an argument.  True 
enough, and powerful enough, but very confusing when the other aspects 
of functional programming with lambda do not hold precisely. Ok, 
functional with caveats.

>> In fact, I have gone out of my way to NOT use
>> lambda because I am fully aware that the BDFL hates it.
>
> In other words, you have had situations where you *could have* used
> lambda, perhaps even *should have* used lambda, but you intentionally
> went *out of your way* (i.e. made the job harder than it needed to be) to
> avoid it, just to slavishly ape the BDFL.

   No, no, no,  not at all. I went out-of-my-way to understand how inner 
functions could do everything lambda does. I use inner functions, not 
lambda, and everything works well; not to ape the BDFL, but because it 
does not appear that lambda is a stable concept, and because I want 
other people "in the normal" category to be able to support or at least 
read my code.

> Thank you for just proving my point for me. Your code would have been
> better, or at least easier, with lambda. Removing it would make the
> language worse, not better.

    No.   see above.

> lambda was designed to allow you to create anonymous functions
> in an expression, not as a statement. There is *no other way* to do that
> in Python.

    Yes, and I agree, and if that were *necessary* we wouldn't be having 
this conversation.  The point is that there is *no* reason that you must 
code your script that way.

> Would you like to discard def as well? No? Why not?

    I am not advocating discarding anything.  I am suggesting that 
lambda is not necessary and might be deprecated without too much 
trouble; if the community sees fit.  That is all.

    Since the name-space and arguably "def" itself is perhaps the most 
powerful aspect of python generally (extensibility) than of course the 
answer to your question is obvious, and discussing it would be silly.

    def is fine,   lambda not so much.  BUT I'm not demanding anyone 
discard it at the moment.  Try to remember Aristotle's mean.


marcus



More information about the Python-list mailing list