RELEASED Python 2.4, alpha 1

Denis S. Otkidach ods at strana.ru
Sat Jul 10 12:41:56 CEST 2004


On Sat, 9 Jul 2004, Michele Simionato wrote:

MS> The simplest workaround is Pythonic in the sense that it is
MS> explicit
MS>
MS> f1,f2,f3=tuple(lambda i=i: i for i in [1,2,3])
MS>
MS> as one explicitly rebinds "i" at each iteration but still I
MS> cannot
MS> find it other than hackish, since there is no point here in
MS> creating
MS> a function with default arguments other than fixing the
MS> binding-in-iteration

You can bind explicitly without default argument hack:
>>> f1, f2, f3 = [(lambda i: lambda: i)(i) for i in [1, 2, 3]]
>>> f1()
1
>>> f2()
2
>>> f3()
3

I think the same will apply to generator expression too.

-- 
Denis S. Otkidach
http://www.python.ru/      [ru]



More information about the Python-list mailing list