Needless copying in iterations?

Ben Finney bignose+hates-spam at benfinney.id.au
Sun Sep 16 01:05:55 EDT 2007


Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:

> In *general* the compiler can't tell, but in specific cases it
> could. A (hypothetical) optimizing compiler would tell the
> difference between:
> 
> for item in alist[1:5]:
>     print item # no possible side-effects

The 'print' statement converts the 'item' to a str. That conversion
could, in a pathological case, have a side-effect (say, if the class
of 'item' had an overridden '__str__' method with side effects), and
the compiler can't know this isn't a pathological case.

> for item in alist[1:5]:
>     alist.append(item) # side-effects DON'T matter

The compiler doesn't know that, at the time of invocation,
'alist.append' doesn't have side effects that matter.

The compiler for a dynamic language like Python has very little
absolute "no significant side-effect in these specific cases"
guarantee of the kind you're assuming even in the cases you choose for
contrast with the ones that *do* have significant side-effects.

-- 
 \          "When we call others dogmatic, what we really object to is |
  `\        their holding dogmas that are different from our own."  -- |
_o__)                                                   Charles Issawi |
Ben Finney



More information about the Python-list mailing list