Python 'for' loop is memory inefficient

Carl Banks pavlovevidence at gmail.com
Mon Aug 17 20:32:29 CEST 2009


On Aug 17, 4:40 am, exar... at twistedmatrix.com wrote:
> On 02:12 am, pavlovevide... at gmail.com wrote:
>
>
>
> >On Aug 16, 3:35 pm, sturlamolden <sturlamol... at yahoo.no> wrote:
> >>On 16 Aug, 14:57, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
>
> >> >         Well, the alternative would be to have two keywords for
> >>looping: one
> >> > for your "simple" incrementing integer loop, and another for a loop
> >>that
> >> > operates over the elements of some collection type.
>
> >>A compiler could easily recognise a statement like
>
> >>   for i in range(n):
>
> >>as a simple integer loop.
>
> >It would be a simple to do if you were writing it for a different
> >langauge was a lot less dynamic than Python is.  It'd be quite a
> >complex hack to add it to CPython's compiler while maintaing the
> >current highly dynamic runtime semantics and backwards compatibility,
> >which is a design constraint of Python whether you like it or not.
>
> In your other message, you said this wasn't a legitimate CPython
> complaint.  Here, you say that it would be a "complex hack" to implement
> this in CPython.  "complex hack" has negative connotations in my mind.
> This seems contradictory to me.

Well, you missed the point, chief.

It's not a legitimate complaint because you can use xrange, you don't
need compiler magic to recognize and optimize range.


Carl Banks



More information about the Python-list mailing list