lambda strangeness??

Alan Gauld alan.gauld at btinternet.com
Sun Feb 27 04:19:40 EST 2005


On Sun, 27 Feb 2005 09:07:28 +0000 (UTC), Alan Gauld
<alan.gauld at btinternet.com> wrote:

> >>> adds = [lambda y: (y + n) for n in range(10)]
> >>> adds[0](0)
> 9
> >>> for n in range(5): print adds[n](42)
> ...
> 42
> 43

> the for loop... It seems to somehow be related to the 
> last value in the range(), am I somehow picking that up as y?

Further exploration suggests I'm picking it up as n not y, if
that indeed is what's happening...

Alan G.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Python-list mailing list