lambda versus "mlambda"...

Andrew M. Kuchling akuchlin at mems-exchange.org
Thu May 11 12:04:30 EDT 2000


Courageous <jkraska1 at san.rr.com> writes:
> Oh, cool! So! Is the portion of the expression where you
> say "self=self" a more general way of introducing a sort
> of closure into the lambda expression? The python documentation

No, it's just creating a function that takes two parameters, and the
second one is named 'self'.  It's given a default value that's the
value of self in the scope where the lambda is being executed, and the
filter/map/whatever will only ever provide 1 argument, so the default
value for self is always used.

This is a general trick for when you need variables inside the
function returned by lambda.  Zope's code uses it a good deal
(actually overuses it, IMHO), as in this bit from Main.py:

    VersionBase.TDB.set_quota(
        lambda x, quota=quota, otherdb=Bobobase._jar.db:
        x + otherdb.pos > quota)

In the above example, they needed the quota and otherdb values inside
the lambda.  
 
-- 
A.M. Kuchling			http://starship.python.net/crew/amk/
The garden of Destiny. You would know it if you saw it. After all, you will
wander it until you die. Or beyond. For the paths are long, and even in death
there is no ending to them.
  -- From SANDMAN: "Season of Mists", episode 0




More information about the Python-list mailing list