Why does not Python accept functions with no names?

Dieter Maurer dieter at handshake.de
Sun Feb 20 12:33:26 EST 2022


Abdur-Rahmaan Janhangeer wrote at 2022-2-20 19:32 +0400:
>Out of curiosity, why doesn't Python accept
>def ():
>    return '---'
>
>()
>
>Where the function name is ''?

Python knows about (somewhat restricted) anonymous functions:
it calls them `lambda` expressions (the body of those functions
can only be an expression).

Your example above can be expressed as
`(lambda: '---')()`.


More information about the Python-list mailing list