Python 'for' loop is memory inefficient

Carl Banks pavlovevidence at gmail.com
Mon Aug 17 04:12:20 CEST 2009


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.

And all this complaining about an issue that can be worked around by
xrange instead of range.  Sheesh.


> In fact, Cython is able to do this.

Cython can do this easily because it is a different language that is a
lot less dynamic than Python.

If you don't care about the dynamic stuff why don't you just use
Cython?  Or quit complaining and just use xrange.


Carl Banks



More information about the Python-list mailing list