[Python-Dev] listcomps vs. for loops

Michael Hudson mwh at python.net
Tue Oct 21 10:45:00 EDT 2003

Jp Calderone <exarkun at intarweb.us> writes:

> On Tue, Oct 21, 2003 at 12:41:00PM +0100, Michael Hudson wrote:
>> Guido van Rossum <guido at python.org> writes:
>> > I don't recall what I said then.  Did I say it was a feature that
>> >
>> >   L = [x for x in R]
>> >   print x
>> >
>> > would print the last item of R?
>> A problem with such code irrespective of anything else is that it
>> fails when R is empty.
>   Not when x is properly initialized.


>  Anyway, this is no different from the
> problem of:
>     for x in R:
>         ...
>     print x

Well, yes.  I still think it's dubious code.

>   In any case, are there plans to also have the compiler emit warnings about
> potential reliance on this feature?

I would hope that we wouldn't make changes without emitting such a
warning.  I'm not sure how hard it would be to implement, tho'.

(It would be /nice/ to implement a warning whenever there's a
possibility of the UnboundLocalError exception, but that *definitely*
requires control flow analysis and that is *definitely* a heap of
work, unless the ast-branch gets some attention).


  We did requirements and task analysis, iterative design, and user
  testing. You'd almost think programming languages were an interface
  between people and computers.                    -- Steven Pemberton
          (one of the designers of Python's direct ancestor ABC)

More information about the Python-Dev mailing list