Needless copying in iterations?
Marc 'BlackJack' Rintsch
bj_666 at gmx.net
Sat Sep 15 20:05:58 EDT 2007
On Sat, 15 Sep 2007 14:58:15 -0700, James Stroud wrote:
> I was staring at a segment of code that looked like this today:
>
> for something in stuff[x:y]:
> whatever(something)
>
> and was wondering if the compiler really made a copy of the slice from
> stuff as the code seems to suggest, or does it find some way to produce
> an iterator without the need to make a copy (if stuff is a built-in
> sequence type)?
The compiler can't "optimize" this as it would change the semantics.
There's no way for the compiler to tell if this copy really is "needless".
`whatever()` may change `stuff[i]` where `i` is in `x:y` and this may lead
to different results wether it iterates over a copy or the original.
Ciao,
Marc 'BlackJack' Rintsch
More information about the Python-list
mailing list