
"Skip Montanaro" <skip@pobox.com> wrote in message news:15964.59840.342254.176499@montanaro.dyndns.org...
For code that wants to cleanly cross the boundary between Python with no boolean type and Python with a boolean type, you sometimes see
True = 1==1 False = 1==0
You get True and False if it didn't exist before and have the added benefit that if it does exist, it gets found in globals() or locals() instead of in __builtins__ and has the right type.
This suggests a more radical optimization that might do more speedup than all the byte-code fiddling currently proposed: automatically localize, at definition time, builtins used within a function. This would be like giving each function a customized implicit set of default args: True=True, None=None, len=len, ...etc. If a program alters __builtins__, def statement placement would determine which version gets used. A non-code-breaking alternative: add a localize() builtin that does the fixup after the fact, something like staticmethod() and classmethod(). If made recursively applicable to classes and modules as well as functions, one could end the setup phase of a long running program with localize(__import__(__name__)) to localize everything A speedup of fraction x at cost of time t would pay if the program runs longer than t/x. (Example: .05 reduction with 1 minute fixup pays after 1/.05 = 20 minutes.) I don't really know, but I imagine that psyco, for instance, would benefit by having builtins localized and detectibly constant (by the lack of any redefinition within the function). As it is now, neither the compiler nor psyco can assume that True, for instance, is not rebound between function invocations. Turning builtins into default locals isolates a function much more than it now is. Well, just some crazy thoughts... Terry J. Reedy