Let's Talk About Lambda Functions!

John Roth johnroth at ameritech.net
Fri Aug 2 00:31:06 CEST 2002


"Bengt Richter" <bokr at oz.net> wrote in message
news:aic4ri$eeh$0 at 216.39.172.122...
> On Tue, 30 Jul 2002 19:22:50 -0400, "John Roth"
<johnroth at ameritech.net> wrote:
>
> >
> >"Ian Bicking" <ianb at colorstudy.com> wrote in message
> >news:mailman.1028054866.6584.python-list at python.org...
> >> On Tue, 2002-07-30 at 11:05, John Roth wrote:
> >> > I tend to agree with you on that one, but it's a matter of style.
I
> >> > observe
> >> > that e.g. Smalltalk style does anonymous code blocks all over the
> >place.
> >>
> >> I've seen people argue that you can do all the same things with
> >> iterators as Smalltalk does with code blocks.  And, really, it's
not
> >as
> >> though Smalltalk has truly novel control structures -- everything
> >still
> >> boils down to while, for, and if.  Smalltalk does give you easy
> >> callbacks, though.
> >>
> >> > There's no reason my proposal couldn't be extended to anonymous
> >> > classes: the syntactic issues are exactly the same. The
difficulty
> >is
> >> > in extending it to methods, as opposed to functions. The only way
> >> > to distinguish a method from a function today is to observe that
> >> > methods are defined at the top level of a class; a def anywhere
> >> > else is a function (I think.)
> >>
> >> A method is just a function that is bound to a class variable.  So
you
> >> can do something like:
> >>
> >> class X:
> >>     pass
> >>
> >> X.func = lambda self, x: x * 2
> >> x = X()
> >> x.func(10)
> >> ==> 20
> >>
> >> 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. Lambdas are not methods
because they aren't directly under the class definition, and a
generalized
"def" also isn't directly under the class definition: it's within
another method
(or possibly list, tuple or dictionary definition.)

That's the rub. You can have it one way or the other - either an
anonymous 'def' found anywhere within a class is a function or a
method, but it can't be either without some other syntax to specify
which.

> It works on Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit
(Intel)] on win32
> However, 'self' in this example is not even used except to get the
> instance bound to it and be ignored.

In thinking about this, and following up on your other suggestion
to use parens to delimit anonymous functions for clarity, we could
use parens for functions, and brackets for methods. This wouldn't
be the clearest syntax in the world, but Python currently distinguishes
between the two by context.

John Roth





More information about the Python-list mailing list