Let's Talk About Lambda Functions!

John Roth johnroth at ameritech.net
Tue Jul 30 19:22:50 EDT 2002


"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.

On the other hand, if 'self' was the name assigned
to the instance in the enclosing method definition, it
would acomplish just about everything required. I suspect
there are a few corner cases it wouldn't handle, to
the confusion of all and sundry.

Someone who knows the language in more depth
than I do would have to comment on that.

John Roth






More information about the Python-list mailing list