[Python-ideas] Syntactic sugar to declare partial functions

Chris Angelico rosuav at gmail.com
Mon Aug 13 14:09:55 EDT 2018


On Tue, Aug 14, 2018 at 4:00 AM, Bruce Leban <bruce at leban.us> wrote:
> And as to saying a lambda function is an "anonymous function": the anonymity
> is not a property of the function. If I assign it to a name, it's no longer
> anonymous. Really a "lambda" or "lambda function" is just a function, but
> "lambda" is a synecdoche for "function created with a lambda expression".

True, but at that point, you get into hairy points of definitions.
Which of these functions is anonymous?

def do_stuff(callback): ...

do_stuff(do_stuff)
do_stuff(lambda: 42)
do_stuff(callback=lambda: 42)

Obviously do_stuff itself has a name. When you pass it a parameter, it
can access that as "callback", which means the function has been
assigned to a name. Does it cease to be anonymous? What if you use a
keyword argument?

Within Python, there's a fairly clear definition: if there is
something in the source code which sets the function's __name__
attribute, it's not an anonymous function. So anonymous functions come
from:

* Lambda expressions, always called "<lambda>"
* Comprehensions/genexps, always called "<listcomp>" etc
* Callable objects that aren't functions (or classes, since those have names)
* Maybe something else that I've forgotten.

You're absolutely right that a "lambda function" isn't really a thing,
in the same way that Python doesn't have "raw strings" (only "raw
string literals", which are literals which result in perfectly
ordinary strings). But the anonymity of them is a somewhat measurable
feature, even if it isn't very important. The distinction between
"lambda functions" and "def functions" is important to style guides,
but otherwise shouldn't matter.

ChrisA


More information about the Python-ideas mailing list