[Python-Dev] listcomp / par-for (summary)

Thomas Wouters thomas@xs4all.net
Wed, 12 Jul 2000 18:26:56 +0200


On Wed, Jul 12, 2000 at 10:40:11AM -0500, Paul Prescod wrote:

> > The range-literal thing ([:10:2] -> [0,2,4,6,8]) seems to be acceptable,
> > I believe ? It might 'merely' be syntactic sugar for a builtin, but it's
> > such pretty sugar ! :-)

> Let me play devil's advocate to make sure this is fleshed out enough.
> 
> How's this for obfuscation:

> [0:10][0:10][0:10]

Sure, but it's not more obfuscated than a long line of map(lambda ...
reduce(filter(map(lambda ... ))) -- it's definately easier to parse for
humans ! The parser won't have any trouble with it, if that's what you mean:

>>> [:200:2][0:20]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38]
>>> [:200:2][0:20][10:20]
[20, 22, 24, 26, 28, 30, 32, 34, 36, 38]

> ---
> And arguing from analogy, shouldn't
> 
> range(0:bignum)[0:10:2] be [0:10:2]

Yes, it should, but there is no way to enforce it, currently, because range
can be shadowed by a local or global variable. And it doesn't have to be
unintended, either. With range literals, that's no longer possible.

> > And it's not possible to shadow the builtin like with range.

> You probably shouldn't be able to shadow most builtins period. 

Why not ? It would introduce a number of problems: adding new builtin
functions would be as cagey as adding new reserved words (in effect, all
builtins *become* reserved words.) So it would break a ton of code. It would
make extending the builtins practically impossible. It would be very
confusing for current Python programmers. Etcetera.

> > (Though I have one possible addition to the range syntax: range tuples,
> > like so:
> > 
> > (20:10:3)

> I presume you meant (10:20:3). Right?

Yes, sorry.

> > They could just generate a rangeobject, like xrange does... That could be a
> > fair bit of memory savings ;) But I have to admit the syntax isn't as
> > obvious as [19:15:-1])

> I don't follow you here. Anyhow, I like the idea that there should be a
> syntax that does the rangeobject thing and it makes sense that it needs
> to be immutable.

Well, when I look at '(10:20:3)', it doesn't look like a range, to me.
Mostly because it has no precedent in Python: ()'s are used for grouping,
and for function calls. They do not create tuples! :) In fact, the
'(10:20:3)' syntax reminds me more of dates or machine-addresses than it
does of ranges.

For that reason I think it's better to live with xrange() ;)

-- 
Thomas Wouters <thomas@xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!