lambda versus "mlambda"...

Kevin Russell krussell4 at
Thu May 11 01:50:35 EDT 2000

It doesn't look like it would be as convenient as your proposed
mlambda, but the "instancemethod" function from the "new"
module will let you create new methods on the fly.

Say for some unknown reason you want to add a first() method
to the UserList class without creating your own subclass.  With your
mlambda, you might be able to do something like

    UserList.first = mlambda x: x[0]

With instancemethod you could do the more convoluted, but

    UserList.first = instancemethod(lambda x: x[0], None, UserList)

If you only want the method to go on a single instance, replace the
None with the instance.  Like:

    myList.first = instancemethod(lambda x: x[0], myList, UserList)

Or if you want every UserList in your program to have special access
to the first element of the myList instance, you could even do:

    UserList.first = instancemethod(lambda x: x[0], myList, UserList)

This is starting to hurt my brain.  My temples are throbbing the same
way they did for the three minutes I actually understood what
currying was all about.  I'm going to stop now.

-- Kevin

Courageous wrote:

> Hi, I'm fairly new to python, so what I'm about to talk about might
> be utterly mistaken, but I'll take a poke at it, anyway. Here goes.
> It occurs to me that "mlambda" might be a nice builtin...
> (I have to interrupt to say that if anyone knows a way of doing this
>  now, please bonk me on the head).
> When I discovered that "pointers to method objects" automatically
> encapsulated the instance object within themselves, I thought that
> was a very cool thing.
> Being exposed to lambda functions from lisp, I also think that, in
> certain contexts, map/lambda expressions are powerful and inter-
> esting.
> However, one of the things that I think would be nice are lambda
> *methods*. That is, if you pass a lambda function off using
> "mlambda" (a builting I just made up), and this lambda function
> is generated in the context of a class or an instance, it would
> behave like a method object callable, automatically encapsulating
> the method (to wit: upon invocation, the passing of "self" is
> assumed).
> If this is something I've missed and it's somehow in there already,
> someone please point it out. Otherwise, I recommend this for
> addition to Python.
> Guido's deprication of map/lambda aside, of course. :)-
> C/

More information about the Python-list mailing list