Is this a good use for lambda

Bengt Richter bokr at oz.net
Sun Dec 19 23:41:36 EST 2004


On Sun, 19 Dec 2004 20:59:43 +0100, "Fredrik Lundh" <fredrik at pythonware.com> wrote:

>Walter S. Leipold wrote:
>
>> I think that Charlie's point is that, when you use "def <name>", you have
>> <name> polluting your namespace.  The whole program becomes harder to
>> understand because you can't ignore <name> anywhere, even if it was only
>> ever intended to be used in one place.
>
>Ahem.  If you name the function, you can reuse the name (or just forget about it)
>as soon as you've used the function object.
>
>If you don't want to reuse the name because you might want to reuse the function
>object, you have to name it anyway.
>
Are you forgetting that all bindings are not directly name bindings as created by def? ;-)
(See also various tkinter-related uses).

 >>> funs = [lambda:'one', lambda:'two', lambda:'three']
 >>> for use in xrange(2):
 ...     for i in xrange(3):
 ...         print '%susing fun[%s] => %r' %('re'*(use>0), i, funs[i]())
 ...
 using fun[0] => 'one'
 using fun[1] => 'two'
 using fun[2] => 'three'
 reusing fun[0] => 'one'
 reusing fun[1] => 'two'
 reusing fun[2] => 'three'

Regards,
Bengt Richter



More information about the Python-list mailing list