Securing a future for anonymous functions in Python

Bengt Richter bokr at oz.net
Fri Jan 7 17:02:19 EST 2005


On 07 Jan 2005 14:38:01 +0100, Jacek Generowicz <jacek.generowicz at cern.ch> wrote:
[...]
>
>[*] Funnily enough, getting them to understand that "lambda x: fn(x)"
>    is just a very silly way of writing "fn", can be quite a struggle
>    at times ... but that's probably a consequence of the context in
>    which lambda is introduced.
Actually, it may _usually_ be silly, but it could be a way of deferring
a name lookup instead of using the current binding of a name:

 >>> def bar(): return 'this is bar'
 ...
 >>> def foo(f=bar): print f()
 ...
 >>> def bar(): return 'this is updated bar'
 ...
 >>> foo()
 this is bar
 >>> def foo(f=lambda:bar()): print f()
 ...
 >>> foo()
 this is updated bar
 >>> def bar(): return 'this updated bar was picked up by silly lambda'
 ...
 >>> foo()
 this updated bar was picked up by silly lambda

Regards,
Bengt Richter



More information about the Python-list mailing list