On Mon, 21 Feb 2022 at 16:48, Avi Gross via Python-list
<python-list@python.org> wrote:
>
> 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.
>

Functions in Python have two different concepts of "name", which
aren't always the same. One is the way that you refer to it; the other
is its __name__ attribute (and related attributes like __qualname__).
An anonymous function doesn't necessarily have to lack a name, per se;
it simply doesn't have a very interesting name:

>>> (lambda: 1).__name__
'<lambda>'
>>> (lambda: 2).__name__
'<lambda>'
>>> (lambda: 3).__name__
'<lambda>'

If you want to refer to them as f[0] etc, they can have arbitrary
names, which may or may not themselves be meaningful. I frequently
build a dictionary or list of functions by decorating standard 'def'
statements:

funcs = []

@funcs.append
def spam(): ...
@funcs.append
def ham(): ...

They still have __name__ attributes, but it's absolutely fine to
double them up, since they're going to be looked up via the list.
(Though there's still value in having at least *some* name on them,
since it shows up in backtraces.)

There's very little reason to have a function that actually doesn't
have a name. It's usually fine to give it an arbitrary and meaningless
name.

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

Reply via email to