Decorator for Binding Globals into Constants

Discussion in 'Python' started by Raymond Hettinger, Sep 9, 2004.

  1. 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.
     
    Raymond Hettinger, Sep 9, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. glomde
    Replies:
    5
    Views:
    525
    glomde
    Mar 29, 2007
  2. stef mientki

    how to get module globals into a class ?

    stef mientki, Dec 9, 2007, in forum: Python
    Replies:
    1
    Views:
    229
    Peter Otten
    Dec 10, 2007
  3. r0g
    Replies:
    3
    Views:
    293
  4. alex23
    Replies:
    7
    Views:
    224
    Jan Kaliszewski
    Aug 22, 2009
  5. Jack Bates
    Replies:
    0
    Views:
    211
    Jack Bates
    Jan 26, 2011
Loading...

Share This Page