About generators

B

Ben Finney

I'm new to python and I really like what I've seen so far with just
one exception; the absence of a nice syntax for ranges of integers.

I'm not sure if you're unaware of the range() builtin:

for i in range(10):
print i

Are you saying this isn't a "nice syntax"? It seems quite
self-explanatory, and doesn't require new syntax.

What is it you don't like about range()?
[I prefer] having "expr1 ... expr2" returning an iterator from expr1
to expr2 (second limit included).

If this were to happen, I'd prefer two periods instead of three
(matching previous experience with Pascal and related languages).
 
A

Andrea Griffini

I'm new to python and I really like what I've seen so far
with just one exception; the absence of a nice syntax
for ranges of integers. I've read PEPs about allowing

for i in 10:
print i

and I must say I don't like it very much, but I didn't
find a discussion about what looks more natural

for i in 0...9:
print i

or

squares = [x*x for x in 1...100]

that is having "expr1 ... expr2" returning an iterator
from expr1 to expr2 (second limit included).

Has this been considered ? If yes (as I suppose) where
can I find and explanation about it ? So far python
seems very logical, and an explanation of this seemingly
illogical absence would help me (I hope) in better
understanding this language.

TIA
Andrea
 
P

Peter Hansen

Andrea said:
I'm new to python and I really like what I've seen so far
with just one exception; the absence of a nice syntax
for ranges of integers. I've read PEPs about allowing

for i in 10:
print i

and I must say I don't like it very much, but I didn't
find a discussion about what looks more natural

for i in 0...9:
print i

or

squares = [x*x for x in 1...100]

that is having "expr1 ... expr2" returning an iterator
from expr1 to expr2 (second limit included).

Has this been considered ? If yes (as I suppose) where
can I find and explanation about it ?

Google Groups is always helpful for such questions:

http://groups.google.com/groups?q=integer+range+syntax&meta=group=comp.lang.python.*

This leads to various threads, such as the "Re: Thoughts on PEP284"
which had the same proposal as yours and what is, I believe, an
adequate reason for rejecting it.

-Peter
 
J

John Roth

Peter Hansen said:
Andrea said:
I'm new to python and I really like what I've seen so far
with just one exception; the absence of a nice syntax
for ranges of integers. I've read PEPs about allowing

for i in 10:
print i

and I must say I don't like it very much, but I didn't
find a discussion about what looks more natural

for i in 0...9:
print i

or

squares = [x*x for x in 1...100]

that is having "expr1 ... expr2" returning an iterator
from expr1 to expr2 (second limit included).

Has this been considered ? If yes (as I suppose) where
can I find and explanation about it ?

Google Groups is always helpful for such questions:

http://groups.google.com/groups?q=integer+range+syntax&meta=group=comp.lang.python.*

This leads to various threads, such as the "Re: Thoughts on PEP284"
which had the same proposal as yours and what is, I believe, an
adequate reason for rejecting it.

As far as I can tell, the appropriate PEP to reference
is 204: range literals. That one proposed using slice
notation, and it was firmly rejected.

I frankly like the idea since .. or ... is an operator, so it
naturally leads to a magic method or two, opening the
door to other types of ranges, such as reals or dates,
etc...

John Roth
 
A

Andrea Griffini

Google Groups is always helpful for such questions:

http://groups.google.com/groups?q=integer+range+syntax&meta=group=comp.lang.python.*

This leads to various threads, such as the "Re: Thoughts on PEP284"
which had the same proposal as yours and what is, I believe, an
adequate reason for rejecting it.

Thanx for the pointer, but I wasn't able to find a discussion
about the use of a "normal" range binary operator; I found
things like

for x in 0<= x < 10:
for x in 10:
for x in integers[0,1,...,10]:
for x in integers[0:10]:

I personally find all those forms not better than (x)range; the
first one looks a test, not a definition of an iteration and in
math it doesn't imply any order, the others are at least as
obscure to me as (x)range.

Something that would be very similar to what is used in math
is probably

for x in 1...10:

May be it's my math background, but I find things like

squares = [x*x for x in 1...100]

quite readable

And yes, I also used to write a lot of Pascal code so the ".."
is more natural to me (I've also to confess that I use PERL,
even if this probably means I'll be mailbombed from readers
of this NG ;-) ).

But in math three are used, and the three-dot ellipsis is
already present in Python (even if I've still to understand
what's used for); so asking by using the three-dot one
seemed more correct to me.

To me it's very strange that the "range operator" approach
hasn't been discussed at least as much as those other (IMO)
much more exotic ones. Is this because it would be like
admitting that there is something good in PERL too ? :-DDD

Sure this would leave open a few problems, like how to specify
the step (how about the BASIC "step" ? it shouldn't create
any syntax ambiguity, I suppose) but those using increments
different from +1 are already less "natural", and a more
convolute syntax (range ?) or an explicit "while" could be
acceptable anyway; looping using +1 and accessing by "[-i]"
or "[i*3]" may be is more readable (and is used often in math).

Ok... too much talking for a newbie, I'm back to reading :)

Andrea


PS: Oh, by the way. Very funny the idea of allowing ++x
with the meaning it has in Python :)
 
P

Peter Hansen

Andrea said:
Thanx for the pointer, but I wasn't able to find a discussion
about the use of a "normal" range binary operator;

You didn't read far enough. The one I mentioned above is item
number 8 in the results from Google. In it, Sean Ross had asked
about "for i in 0...10" and David Eppstein replies and points
out the key problem with it.
Sure this would leave open a few problems, like how to specify
the step (how about the BASIC "step" ?

Yes, exactly the problem he pointed out. He goes on to say
"If that's all the loop syntax can do, why not just keep the
current "for i in range(10):" syntax?
"Explicit is better than implicit."

And I agree with him. Don't introduce extra syntax for trivial
gain...

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,786
Messages
2,569,626
Members
45,328
Latest member
66Teonna9

Latest Threads

Top