On Thu, 05 Jul 2007 19:14:07 +0000, Dan wrote:

> So, I think I understand what python's scoping is doing in the
> following situation:
>>>> x = [ lambda: ind for ind in range(10) ]
> 
> […]
> 
> But, I'm wondering what is the easiest (and/or most pythonic) way to
> get the behavior I want? (If you haven't guessed, I want a list of (no
> parameter) functions, each of which returns its index in the list.)

Default arguments are evaluated when the function is defined:

In [15]: x = [lambda x=i: x for i in xrange(10)]

In [16]: x[0]()
Out[16]: 0

In [17]: x[5]()
Out[17]: 5

Ciao,
        Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to