Let's Talk About Lambda Functions!

John Roth johnroth at ameritech.net
Wed Jul 31 09:35:40 EDT 2002


"Ian Bicking" <ianb at colorstudy.com> wrote in message
news:mailman.1028076887.27215.python-list at python.org...
> On Tue, 2002-07-30 at 18:22, John Roth wrote:
> > > 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.
>
> Fortunately it does work!  You didn't read my first sentence.  Those
> (working) examples don't rely on the name "self" in the argument list.
> They rely on the fact that a function is being put in a *class*
> attribute (this doesn't work the same way if you put it in an instance
> attribute).

I'm not certain of context here, because your reply didn't
associate properly with my original post, so if I wander a bit,
please excuse.

The issue I was addressing was putting an anonymous function
(or method) in an expression, without any fancy conditions. In other
words, putting it where you would normally want to - with lots
of other code within a class. I wasn't addressing a corner condition.

When you're writing a method in a class, and you want to
create an anonymous function within the code of that method,
how do you tell the compiler that you want a method instead?
That is the question.

It may turn out to have a trivial answer, but as I think I said in
the original post, my knowledge of the details of Python at
that level is insufficient. What I am certain of is that simply
saying 'self' as the first parameter isn't sufficient for the
compiler to mark it as a method (which is a different kind
of object than a function!)

What I'm not certain of is whether it matters.

John Roth
>
>   Ian
>
>
>





More information about the Python-list mailing list