[Python-Dev] accumulator display syntax

Guido van Rossum guido at python.org
Mon Oct 20 12:37:17 EDT 2003

> On Monday 20 October 2003 04:30 pm, Guido van Rossum wrote:
> > > We are indeed sure (sadly) that list comprehensions leak control variable
> > > names.
> >
> > But they shouldn't.  It can be fixed by renaming them (e.g. numeric
> > names with a leading dot).
> Hmmm, sorry?
> >>> [.2 for .2 in range(3)]
> SyntaxError: can't assign to literal
> I think I don't understand what you mean.

I meant that the compiler should rename it.  Just like when you use a
tuple argument:

   def f(a, (b, c), d): ...

this actually defines a function of three (!) arguments whose second
argument is named '.2'.  And the body starts with something
equivalent to

   b, c = .2

For list comps, the compiler could maintain a mapping for the listcomp
control variables so that if you write

  [x for x in range(3)]

it knows to generate bytecode as if x was called '.7'; at the bytecode
level there's no requirement for names to follow the identifier syntax.

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

More information about the Python-Dev mailing list