Decorator for Binding Globals into Constants

Raymond Hettinger python at
Thu Sep 9 04:16:09 EDT 2004

FWIW, I've recast the constant binding recipe into decorator form:

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:

    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.


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.

More information about the Python-list mailing list