scoping with lambda in loops

martin z pxtl at hotmail.com
Tue Sep 16 22:18:18 EDT 2003


> a = []
> for index in range(5):
>     a.append(lambda index=index: index)
>
> or maybe more concisely
>
> a = [lambda index=index: index for index in range(5)]

You know how Python is supposed to be executable pseudocode?  Well that
stuff is farking ugly.  If I handed pseudocode like that into any TA in one
of my classes, I'd be toast.  Is there any way to do that in a legible
manner?






More information about the Python-list mailing list