itertools comments [Was: Re: RELEASED: Python 2.3a2]

Alexander Schmolck a.schmolck at gmx.net
Fri Feb 21 19:48:06 EST 2003


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

> Alexander Schmolck <a.schmolck at gmx.net> writes:
> > > >> Not without breaking, e.g., xrange(23, 17923, 12)[15] -- which now
> > > >> has a perfectly good result of 203 and would break if xrange
> > > >> returned an iterator (or any other non-indexable object).
> > > > 
> > > > It could always return and indexable iterator.
> > > 
> > > It would only seem to break such usage as:
> > > 
> > > N_times = xrange(N)
> > > 
> > > for i in N_times:
> > >     for j in N_times:
> > >         process(i, j)
> > 
> > Excellent point. So it is not an option.
> 
> Yuck, I didn't you could do that with xrange.  It's surprising and
> somewhat bizarre.

Well, I wouldn't disagree that xrange is a bit of a freak (BTW can anyone tell
me why there is a need for both `slice` and `xrange`?) but given that xrange
was intended as a lazy equivalent to range, this behavior maybe shouldn't be
too surprising (if N_times where just a normal list, generated by range, then
clearly it would still have the same semantics).

alex




More information about the Python-list mailing list