Let's Talk About Lambda Functions!

John Roth johnroth at ameritech.net
Fri Aug 2 14:01:50 CEST 2002

"Greg Ewing" <see_reply_address at something.invalid> wrote in message
news:3D49E138.80202 at something.invalid...
> John Roth wrote:
> >>>>In fact, you can even do:
> >>>>
> >>>>class X:
> >>>>    func = lambda self, x: x * 2
> >>>>
> >>>Unfortunately, that won't work. The word 'self' is not
> >>>magic - using it doesn't convert a function to a method.
> >>>
> >>>
> >>No, but making it a class attribute seems to do the magic
> >>(which can also be modified with staticmethod and  classmethod).
> >
> > Yes, but that's the exact problem. Methods become such by
> > being directly under the class definition.
> No, they don't! They become methods by being builtin
> function objects and getting found in the class namespace
> during an attribute lookup. Where abouts the definition
> was originally written doesn't come into it.

[example snipped]

Interesting! So an anonymous def is necessarily
a function.

In other words,

class foo:
    bar = (
        def (x, y):
            print x
            print y

makes the anonymous function a method because it's
bound to a name in the class namespace? (I'm using
Bengt Richter's suggested syntax here, rather than my
original suggestion, btw.)

John Roth

More information about the Python-list mailing list