[Python-3000] range() issues

Alexander Belopolsky alexander.belopolsky at gmail.com
Wed Apr 30 05:04:57 CEST 2008


Correction: My calculation below was only correct for N = 1 case.  In
general, the two alternatives will create N+2 vs. N+1 auxiliary
objects.

On Tue, Apr 29, 2008 at 10: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!
>


More information about the Python-3000 mailing list