Decorator for Binding Globals into Constants

  • Thread starter Raymond Hettinger
  • Start date
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top