Scott David Daniels wrote: > Blackbird wrote: >> I think >> >> for i in range(10): >> <some code that depends on i> >> >> is more readable than a while loop with explicit incrementation of i. > >> However, range(10) in the command interpreter obviously returns a >> list. Is this list optimized away in the code above, or is it >> actually constructed internally? (With, say, CPython in one of the >> recent versions.) > > Yup, and if you are tuning a piece of code to the wall, you should > time it and possibly care. Likely you are not, and the timing makes > no difference. Someday, range will behave like xrange automagically, > and in the meantime your code will read just fine. If you are in > trouble now, your code reads much more like: > for i in range(100000): > ...
Thanks. Yes, this will be a problem for iterations in the order of 10**5 and upwards only, and those are rare in most applications. So my question was more motivated by a general curiosity about the inner workings of the Python interpreter, and I sort of understand why advanced optimization on an instruction set like bytecode would be difficult. And in the mean time, I found this insightful piece: http://www.python.org/doc/essays/list2str.html -- http://mail.python.org/mailman/listinfo/python-list