crazy lambdas
Remco Gerlich
scarblac at pino.selwerd.nl
Wed Feb 14 05:22:26 EST 2001
Dan Parisien <dan at eevolved.com> wrote in comp.lang.python:
> e.callbacks.append(lambda d=dict, p=person: del d[p])
>
> # .... later
>
> e.trigger() #deletes
> ---
> those who use lambdas alot will notice my mistake. I, however, do not know
> what I am doing wrong. I think it has something to do with the fact 'del
> d[p]' doesn't have a value...
The problem is that it is a statement, not a function. Note that you don't
use parentheses after del either, just like with print, import, exec, etc.
Assignment isn't a function either.
> Is it possible to do what I want (make a custom one-time-use function that
> is called much later and deletes a key from a dictionary)?
Make the function with a normal def. Make another function that creates
those functions so you don't have to do it all the time...
def make_deleter(dict, person):
def deleter(dict=dict, person=person):
del dict[person]
return deleter
e.callbacks.append(make_deleter(dict, person))
--
Remco Gerlich
More information about the Python-list
mailing list