R
Raymond Hettinger
FWIW, I've recast the constant binding recipe into decorator form:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/277940
Now, optimizing global access is as easy as attaching a decorator.
If you used to write:
def myfunc(data, _len=len):
. . .
You can now write:
@bind_constants()
def myfunc(data):
. . .
Any references to len() or other globals will be made local. This can
optimize module globals and recursive functions as well. It also
works on methods defined in class definition blocks.
Enjoy,
Raymond Hettinger
P.S. When writing @bind_constants(), be sure to include the
parentheses. There's a big difference between decorators that take
arguments and those that don't. This one does.
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/277940
Now, optimizing global access is as easy as attaching a decorator.
If you used to write:
def myfunc(data, _len=len):
. . .
You can now write:
@bind_constants()
def myfunc(data):
. . .
Any references to len() or other globals will be made local. This can
optimize module globals and recursive functions as well. It also
works on methods defined in class definition blocks.
Enjoy,
Raymond Hettinger
P.S. When writing @bind_constants(), be sure to include the
parentheses. There's a big difference between decorators that take
arguments and those that don't. This one does.