[Python-Dev] AST optimizer implemented in Python

Stefan Behnel stefan_ml at behnel.de
Sun Aug 12 06:42:25 CEST 2012


Chris Angelico, 12.08.2012 01:22:
>> Other idea to improve this optimizer:
>>  - move invariant out of loops. Example: "x=[]; for i in range(10):
>> x.append(i)" => "x=[]; x_append=x.append; for i in range(10):
>> x_append(i)". Require to infer the type of variables.
> 
> But this is risky. It's theoretically possible for x.append to replace
> itself. Sure it may not be a normal or common thing to do, but it's
> possible.

Not only that. It changes semantics. If x.append is not defined, the
exception would now be raised outside of the loop, and the loop itself may
have side-effects already. In fact, the mere lookup of x.append may have
side effects as well ...

Stefan




More information about the Python-Dev mailing list