Generator expressions v/s list comprehensions

Alex Martelli aleaxit at yahoo.com
Mon Aug 30 05:59:41 EDT 2004


Paul Rubin <http://phr.cx@NOSPAM.invalid> wrote:

> aleaxit at yahoo.com (Alex Martelli) writes:
> > It would sure be nice if Python had been born back from day one with all
> > the neat features it has taken years to develop -- that way we wouldn't
> > have any issues that are there just because of backwards compatibility.
> > 
> > Unfortunately, this wish is totally unrealistic -- obviously people do
> > come up with cool ideas such as the iterator protocol, and generators,
> > after the language has been around for a while. 
> 
> And yet a lot of the unrealisticness comes directly from the Python
> culture.  Maybe it took a while to think up the iterator protocol, but
> what about nested scopes?  What about the += operator?  How about the
> sorted() method on lists?  One could go on and on with more examples
> like that.  

...and some of the examples would be wrong, just like yours about "the
sorted method on lists" -- sorted is a new built-in function in 2.4, not
a method on lists, and it accepts any sequence, not just a list.

> These things exist in other languages and had been
> requested in Python for years before they got accepted.  And every

A million other features satisfy this description D: they exist in other
languages and have been requested in Python for years.  Indeed, it's
hard to think of ANY feature which exists in other languages and has NOT
been requested for Python.

> time some little thing gets added changing the language, that creates
> a new mini-dialect that users have to remember for a while and then
> forget.  The result is "version fatigue"; one gets bleary trying to
> remember what's in the language this week.  Those features are in
> other languages for a reason, and there's been enough experience using
> them (in those languages) that their desirability for Python should
> never have seriously been in question.  So it would have been better
> to include them from the beginning, instead of through separate
> episodes of prolonged agony for each one.

If Python included every features satisfying description D, then Python
would be the largest, bulkiest, most unwieldy language in the world --
in fact I think even including half the features in D would be plenty to
make Python completely unusable.  That a dozen features satisfying D
have indeed been included is therefore a silly argument, considering
that a gross more features satisfying D have not been included and
hopefully never will.  Moreover, the specific feature we're discussing,
generator expressions, doesn't really satisfy D (what Haskell has is not
quite the same thing, you know), making your argument even weirder.

All features that are in any programming language are there for a reason
(not necessarily a good one -- some languages may have been designed on
principles close to those implied by this silly criticism you are
expressing, for example) and, for old enough languages, there has been
enough experience using them.  Nevertheless it's perfectly proper and
indeed wise to seriously question their desirability for Python.  I bet
that if you took a feature satisfying description D, you'd find a chance
of more than 90% that it would be totally silly to have it in Python and
that it will never be there.

Finding out what dozen, out of dozens of dozens, of features satisfying
D, would work well with Python, and which ones wouldn't, is obviously
not something that can be rushed.  Sure, introducing some feature in
every release is not ideal, but there is really no alternative that is
even in the least sensible at all.  So, I find your criticisms
unfounded.


Alex
 



More information about the Python-list mailing list