functional vs procedural list comprehension

Peter Dobcsanyi petrus at pobox.com
Fri Sep 7 15:20:17 EDT 2001


In the thread about "Performance of list comprehensions vs. map" some
questions have been raised by Chris Barker about the behavior of list
comprehensions.

Chris Barker <chrishbarker at home.net> wrote:
> Skip Montanaro wrote:
...
>> but in my opinion (and apparently in
>> the opinion of the various powers that be), that would be wrong.

[that is to have list comprehensions behave functionally]

> I guess I am having a hard time seeing why that would be wrong. I just

Me too.

...
> took a look at the PEP, and having been implimented already, it is
> pretty sparce, but I see:
> """
> Rationale
>     List comprehensions provide a more concise way to create lists in
>     situations where map() and filter() and/or nested loops would
>     currently be used.
> """
> except map() and filter work differently than nested loops do, as we are
> talking about here. Which functionality should be emulated?

[ other details of how list comprehensions work currently skipped 
  since they are irrelevant to how they *should* work ]

> Your example did make it clear how that kind of re-binding can be used,
> and I can imagine it would be a useful feature (though I sure can't
> figure out a use now!). HOWEVER, YOU COULD ALWAYS USE FOR LOOPS, IF YOU
> WANT THAT, AND IT WOULD BE MORE CLEAR. HAVING THE BINDINGS OF THE
> EXPRESSION IN A LIST COMPREHENSION FIXED WHEN THE EXPRESSION IS
> EVALUATED THE FIRST TIME WOULD PROVIDE A LOT LESS POTENTIAL FOR
> CONFUSION,
[emphasis by me]

Personally, I would also be in favor of "functional" list comprehensions
in contrast to the current "procedural" ones.

I would be interested in the reasons for the procedural choice and
arguments for functional vs procedural behavior.

Thanks,
               ,
    Peter Dobcsanyi



More information about the Python-list mailing list