why is this different?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Fri Dec 8 10:46:07 EST 2006


In <elbrpk$53i$1 at news2.rz.uni-karlsruhe.de>, Schüle Daniel wrote:

> I have two more examples, but now I understand the difference
> 
> In [70]: x = [eval("lambda:i") for i in range(10)]
> In [71]: y = [eval("lambda:%i" % i) for i in range(10)]
> 
> I think [71] is most obvious what the programmer intends

But unnecessary use of `eval()` is evil.  You can spell it this way:

z = [lambda x=i: x for i in range(10)]

The `x` is a local name and default values are evaluated and bound when
the function is created.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list