[Python-3000] Efficient LC's which don't leak the iteration variables (was Re: Py3k release schedule worries)

Guido van Rossum guido at python.org
Wed Dec 20 22:01:31 CET 2006

On 12/20/06, Jim Jewett <jimjjewett at gmail.com> wrote:
> On 12/20/06, Guido van Rossum <guido at python.org> wrote:
> > On 12/20/06, Nick Coghlan <ncoghlan at gmail.com> wrote:
> > > I haven't actually tried to implement that, though, and I suspect things will
> > > get a little tricky when it comes to dealing with nested scopes like '[(lambda
> > > i=i: i) for i in range(10)]'.
> > That doesn't strike me as tricky; I expect it'll happen automatically
> > if you do the right thing.
>     [(lambda i=i: i) for i in range(10)]
> =>
>     [(lambda i=0_i: i) for 0_i in range(10)]
> The argument to lambda can't be renamed, because it could be called as
> a keyword.

Well duh, the renaming must observe the scope of the variable.
Butthat's no big deal since we observe the scope when generating code
for it anyway (references to locals need to use different bytecode
than globals, and references to variables from outer nested scopes
need to use cell references). Perhaps you were thinking of a textual
substitution? That's not at all how one would do this.

> Knowing which i's to rename strikes *me* as tricky, to the point where
> I would suggest at least considering a "works as implemented"
> pronouncement.  This (along with further nesting, closure variables,
> conditionally defined variables, etc) is obscure enough that backwards
> incompatibility could be justified, if it made the explanation (or
> implementation) simpler.

I have no idea what you mean by "works as implemented". Do you propose
*not* to fix the scope bleed of list comprehension loop control
variables? That's not an option.

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-3000 mailing list