[Python-Dev] Release of astoptimizer 0.3

Victor Stinner victor.stinner at gmail.com
Tue Sep 11 14:06:24 CEST 2012


>> * Call builtin functions if arguments are constants. Examples:
>>
>>   - len("abc") => 3
>>   - ord("A") => 65
>
> Does it preserve python semantics? What if you change the len builtin?

This optimization is disabled by default (in the version 0.3), because
builtin functions may be shadowed. Examples: "len=ord;
print(len('A'))" or exec(compile("print(len('A'))", "test", "exec"),
{'len': ord}).

If you know that one specific builtin function is shadowed (ex:
print), you can modify the configuration to enable optimizations on
builtin functions except the specified function.

Do you projects where builtin functions are shadowed?

The idea is that the author of the application knows its application
(... and all modules used by applications) and is able to explicitly
specify what is that "constant". You can for example declare some
variables as constant using Config.add_constant("module.name", value).
In the same manner, you can declare "pure" functions (without border
effect).

Victor


More information about the Python-Dev mailing list