By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,985 Members | 1,575 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,985 IT Pros & Developers. It's quick & easy.

About generators

P: n/a

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
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Mon, 26 Apr 2004 06:30:10 GMT, Andrea Griffini wrote:
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).

--
\ "Every decent man is ashamed of the government he lives under." |
`\ -- Henry L. Mencken |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #2

P: n/a
Andrea Griffini wrote:
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=in....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
Jul 18 '05 #3

P: n/a

"Peter Hansen" <pe***@engcorp.com> wrote in message
news:L-********************@powergate.ca...
Andrea Griffini wrote:
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=in....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
-Peter

Jul 18 '05 #4

P: n/a
On Mon, 26 Apr 2004 09:42:18 -0400, Peter Hansen <pe***@engcorp.com>
wrote:
Google Groups is always helpful for such questions:

http://groups.google.com/groups?q=in....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 :-)
Jul 18 '05 #5

P: n/a
Andrea Griffini wrote:
On Mon, 26 Apr 2004 09:42:18 -0400, Peter Hansen <pe***@engcorp.com>
wrote:
Google Groups is always helpful for such questions:
http://groups.google.com/groups?q=in....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;


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
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.