Why does not Python accept functions with no names?

Avi Gross avigross at verizon.net
Mon Feb 21 00:46:53 EST 2022


Amusingly, Greg, if you had a system where the un-named anonymous function was to be named the unique value of the empty string, then a second such anonymous function definition would over-write it, as with any named function. The kind of anonymous function we are more used to might be something you can create as say elements of a list so you have a list of functions you can access as in f[0] but in a sense that has a name as it can be accessed as a component of the data structure called f.  I am not sure if python would trivially let you create that. But the point is if you want to be able to make many such pseudo-anonymous functions, in the end, there can only be one.


-----Original Message-----
From: Greg Ewing <greg.ewing at canterbury.ac.nz>
To: python-list at python.org
Sent: Sun, Feb 20, 2022 5:17 pm
Subject: Re: Why does not Python accept functions with no names?

On 21/02/22 6:27 am, Abdur-Rahmaan Janhangeer wrote:
> Well Python deliberately throws an exception if we do not
> pass in a function name. Just wanted to know why it should. As the
> above case is a 100% pure anonymous function.

The syntax for a function definition is defined in the grammar
as requiring an identifier. An identifier in turn is defined
as consisting of at least one character. So the grammar would
need to be changed to make the name optional. Also, the
compiler would need a special case to treat a missing name
there as though it were an empty string.

So again, why *should* it be allowed, given that the parser and
compiler would have to go out of their way to treat it as a
special case, only to create a function that there is no
easy way to call?

BTW, this is not what is usually meant by the term "anonymous
function". An anonymous function is one that is not bound
to *any* name. The thing you're proposing wouldn't be
anonymous -- it would have a name, that name being the empty
string.

-- 
Greg
-- 
https://mail.python.org/mailman/listinfo/python-list


More information about the Python-list mailing list