Let's Talk About Lambda Functions!
see_reply_address at something.invalid
Thu Aug 1 21:32:40 EDT 2002
John Roth wrote:
>>>>In fact, you can even do:
>>>> 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.
This is easily verified:
Python 2.2 (#1, Jul 11 2002, 14:19:37)
[GCC 3.0.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f1 = lambda self, x: x * 2
>>> class C:
... m1 = f1
... m2 = lambda self, x: x + 3
>>> c = C()
This works because lambda expressions create the same type
of function object as def statements do.
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
More information about the Python-list