[Tutor] weird lambda expression -- can someone help me understand how this works

eryksun eryksun at gmail.com
Sat Dec 14 11:18:13 CET 2013


On Sat, Dec 14, 2013 at 4:27 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> Sorry, I don't think that is precise. lambda is not the name of the
> function. You can't use lambda to access the function(s) or treat it
> like any other kind of name in Python. In fact if you try to use it as a
> name you'll likely get a syntax error.
>
> lambda is the key word that defines the function. But its no more
> the name of the function than def is.

As Peter stated, Steven is referring to the function's __name__. In
CPython, the name is also set in the code object:

    >>> f.__code__.co_name
    '<lambda>'

In 3.3, __qualname__ gives the context for a closure:

    def f():
        return lambda: None

    >>> g = f()
    >>> g.__qualname__
    'f.<locals>.<lambda>'

Of course you can set (but not delete) both attributes:

    >>> g.__name__= 'g'
    >>> g.__qualname__= 'f.<locals>.g'
    >>> g.__name__
    'g'
    >>> g.__qualname__
    'f.<locals>.g'


More information about the Tutor mailing list