Why not real anonymous functions?

The official list archives:

The Python-list Archives
The Python-ideas Archives
Mailman 3 Python-Dev - python.org

Unfortunately, the first two cannot be directly searched, but python-list was linked to a google group which google indexes for web searches. Not sure about the other two.

Many of the discussions included non-lambda syntax proposals.

The points you seem to have missed are:

  1. Anonymous functions are a bit defective, as I partially explained in my previous post. But you are not really asking for anonymous functions. To get that, we would just need to make the name part of def statements optional. But this would be useless as there would be no usable reference to the new function and it would be deleted. So you are really asking for function expressions that result in a reference that can be passed, returned, otherwise used to call the function. For this purpose, the __name__ attribute of the function is irrelevant. While lambda expression functions are anonymous in the sense of not being bound to any name in any namespace, they do have the __name__ attribute <lambda>. The same would be done
    with any alternate syntax.

  2. Lambda expressions within a loop seem to be more more bug-prone than equivalent def statements outside the loop. My guess is that people are less apt to see that function code situated within a loop body is disconnected from the rest of the loop than when the same function code is somewhere output the loop in a def statement. It is easy to think the same for any expression syntax.

  3. If one wants the full power and flexibility of def statements, the easiest thing to do is to use a def statement, nested or otherwise. Learning an alternate syntax to do the same is an extra burden. Even restricted lambda statements are an extra learning chore.

  4. Python is quite intentionally a statement and expression language, not an expression-only language like Lisp, which is where possibly anonymous function expressions came from. It does not need every statement fully duplicated as expressions. Like the function code in lambda expressions, the conditional code in conditional expressions is restricted to expressions, not a suite of statements. Comprehensions are similarly equivalent to loop statements with a restricted loop body, in particular newlist.append(item), newset.add(item), newdict.__setitem__(key, value) or yield item. In all these cases, if you want the full expressive power of the statement form, a sensible answer is to use the statement form.

8 Likes