Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Decorator for Binding Globals into Constants

Reply
Thread Tools

Decorator for Binding Globals into Constants

 
 
Raymond Hettinger
Guest
Posts: n/a
 
      09-09-2004
FWIW, I've recast the constant binding recipe into decorator form:

http://aspn.activestate.com/ASPN/Coo.../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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
method-to-instance binding, callable generator decorator Jack Bates Python 0 01-26-2011 05:04 PM
Re: Using 'apply' as a decorator, to define constants alex23 Python 7 08-22-2009 08:11 PM
How to deal with globals during refactoring classes into separatefiles. r0g Python 3 11-19-2008 01:02 PM
how to get module globals into a class ? stef mientki Python 1 12-10-2007 09:00 AM
Why doesnt __getattr__ with decorator dont call __get_method in decorator glomde Python 5 03-29-2007 02:48 PM



Advertisments