Let's Talk About Lambda Functions!

Ian Bicking ianb at colorstudy.com
Tue Jul 30 20:53:38 EDT 2002


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

  Ian






More information about the Python-list mailing list