[Python-3000] range() issues

Guido van Rossum guido at python.org
Wed Apr 30 16:02:20 CEST 2008


On Tue, Apr 29, 2008 at 7:53 PM, Alexander Belopolsky
<alexander.belopolsky at gmail.com> wrote:
> On Tue, Apr 29, 2008 at 10:36 PM, Guido van Rossum <guido at python.org> wrote:
>  ..
>  >  There are good reasons for having range() return an Iterable and not
>  >  an Iterator; e.g.
>  >
>  >  R = range(N)
>  >  for i in R:
>  >   for j in R:
>  >     ....
>
>  You realize that in the snippet above whatever cycles you save by
>  creating R once, you give away by creating iter(R) twice.  So compared
>  to range() returning an iterator and having to write
>
>  for i in range(N):
>   for j in range(N):
>  ...
>
>  you have 3 vs. 2 auxiliary objects created.  And how often do you see
>  code that will not benefit from being generalized from square to
>  rectangular matrices?
>
>  Lots of C code will go away if we nix the range object and leave only
>  rangeiterator!

That's completely besides the point. The point of the example is that
the *Python* code doesn't have to write range(N) twice.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list