[Python-Dev] List comprehensions
Thu, 13 Jul 2000 21:51:59 +0200
Paul Prescod wrote:
> "M.-A. Lemburg" wrote:
> > ...
> > Is it really necessary to have all the "code" inside the square
> > brackets ?
> That's what a list comprehension is! Check the defacto PEP:
> I take it you are against list comprehensions.
If it means writing code in square brackets: yes --
I don't see any real benefit other than confusing the reader...
it doesn't even gain any performance (I would have thought that
constant ranges like [0:10] would generate [0,1,2,3,4,5,6,7,8,9]
and make this a constant, but that doesn't seem to be the case).
> > I always thought of list comprehension as a fast way to
> > just throw together a list, not as a way to "program" the
> > list contents. IMHO, that should be done using explicit
> > statements in the way Skip describes above.
> > I would be completely satisfied with e.g. [0..10], [0..10:2]
> > and [10..0].
> Those are not list comprehensions but "range literals."
Oh, so I won't get these with list comprehension ? Hmm, then
I'm posting on the wrong thread, I guess ;-)
> Actually, the
> syntax we are using is [0:10], [0:10:2], [10:0]. I prefer dotdot, but
> Guido chose the colon a decade ago.
Say, wouldn't it make more sense to
have these notations generate tuples (which are immutable and
could thus be stored as constants) rather than lists ?!
for x in (0:10):
Hmm, looks strange, perhaps not such a good notation....
for x in (0..10):
for x in 0..10:
Oh, now this looks much better :-)
The for-loop opcode could even make some use of this by not generating
a tuple at all (it could use a special mutable counter like the
one available in my speedup patch for 1.5:
[see the ceval part of the patch -- it uses the counter object
the patch also contains which provides a mutable integer
implementation for just this purpose]
The patch also includes some other performance enhancements
such as a small dictionary enhancement and many other things
which have already made it into 2.0)
Python Pages: http://www.lemburg.com/python/