Home

Ryan Ginstrom <software <at> ginstrom.com> writes:
> How about:
>
> >>> def make_adder(i):
> def adder(x):
> return x+i
> return adder
>
> >>> funcs = [make_adder(i) for i in xrange(10)]
> >>> print [func(10) for func in funcs]
> [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
> >>>

Or if you want a one liner:

funcs = [(lambda i: lambda x: x+i)(i) for i in xrange(10)]

Stéphane

previous
next

Re: Put a stl container in private or public?
undo/redo algo
Get Only the Last Items in a Traceback
ANN: UliPad 3.7 released!
Re: strings (dollar.cents) into floats
Fundacja Avalon
Akogo
Fundacja Iskierka
Niechciane i Zapomniane
Krwinka