[Python-Dev] listcomps vs. for loops

Skip Montanaro skip at pobox.com
Mon Oct 20 14:32:13 EDT 2003


    >> ?!?!  When listcomps were introduced, you were strongly against any
    >> changes that would make it difficult to switch back and forth between
    >> a listcomp and its corresponding equivalent for loop.

    Guido> I don't recall what I said then.  Did I say it was a feature that

    Guido>   L = [x for x in R]
    Guido>   print x

    Guido> would print the last item of R?

I suspect the lack of a PEP at the time list comprehensions were added to
the language allowed this to slip through.  PEP 202 was mostly written after
list comprehensions were checked into CVS I think (opened 2000-07-13, marked
final 2001-08-14, yes 2001!).  At just 84 lines it's one of the shortest
PEPs.  The patch I opened on SF (#400654, opened 2000-06-28, closed
2000-08-14) was essentially Greg Ewing's experimental patch, which relied
heavily on the existing for loop code generation.  Had there been a PEP with
the usual fanfare, I suspect we'd have caught (or at least considered)
variable leakage, and perhaps suppressed it.  I don't recall the topic ever
coming up until after list comps were part of the language.  It certainly
seems to be the most controversial aspect, after one accepts the idea of
adding them to the language.  Missing such an obvious point of contention is
perhaps one of the strongest arguments for the current PEP process.

Skip



More information about the Python-Dev mailing list