When you compile the expression

    for i in range(1000):
        pass

does Python make an iterator for range(), and then generate the values
on the fly?  Or does Python actually allocate the list [0, 1, 2, ..., 999]
and then step through it?  

I was under the impression that recent releases of Python optimize this
case, but upon reflection, I have no idea where I got that impression.

If Python actually allocates the list, then clearly we should all use
"for i in xrange".  But "for i in range" looks cleaner, and is potentially
more lightweight than xrange.  Of course, if you want to write code that
runs well on older versions of Python, you need to use xrange().


If Python doesn't currently optimize this case, is there any chance this
optimization could be added?

P.S. It looks like all the cool people look at the generated bytecodes to
answer questions like this one.  I want to be cool too.  Where can I find
information about how to get a bytecodes listing for my compiled Python?

-- 
Steve R. Hastings    "Vita est"
[EMAIL PROTECTED]    http://www.blarg.net/~steveha

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to