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. Advertisements

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:
    736
    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:
    284
    Peter Otten
    Dec 10, 2007
  3. r0g
    Replies:
    3
    Views:
    437
  4. Gustavo Narea
    Replies:
    14
    Views:
    1,133
    Gustavo Narea
    Feb 16, 2009
  5. alex23
    Replies:
    7
    Views:
    339
    Jan Kaliszewski
    Aug 22, 2009
  6. Jon Clements
    Replies:
    8
    Views:
    339
    Steven D'Aprano
    Mar 13, 2010
  7. Phlip
    Replies:
    2
    Views:
    400
    Phlip
    Oct 23, 2010
  8. Jack Bates
    Replies:
    0
    Views:
    276
    Jack Bates
    Jan 26, 2011
Loading...