Lambda going out of fashion

Alex Martelli aleaxit at yahoo.com
Thu Dec 23 07:15:19 EST 2004


Fredrik Lundh <fredrik at pythonware.com> wrote:

> Alex Martelli wrote:
> 
> > I think I'll scream, though not quite as loud as for my next seeing:
> >
> > map(lambda x: f(x), ...
> >
> > instead of
> >
> > map(f, ...
> 
> note that if you replace "map" with "some function that takes a callable",
> the difference between these two constructs may be crucially important.

Sure -- if global name f gets rebound during the execution of the ``some
function'' (or if that function stashes the callable away, and that name
gets rebound later, etc), the semantics of passing f are different -- to
get the same semantics with a lambda, you'd have to use the old trick:

somefunc(lambda x, f=f: f(x), ...


I don't recall ever having seen the late-binding semantics (of the
lambda I originally showed) ``used properly'' -- I _have_ sometimes seen
that semantics cause subtle bugs, though.  Do you have any real-life
examples where expecting and allowing for rebinding of global name `f'
is proper and desirable behavior?  Being able to show a "good" use of
this late-binding could be helpful, if I knew of any.


Alex



More information about the Python-list mailing list