I've learned. I'll share.

November 11, 2008

Easy Python Decorators with the decorator decorator

Decorators in python are very powerful, but are often a pain to get right, especially when you want to pass arguments to the decorator. Typically, it involves defining a function in a function in a function, etc, up to 4 layers deep. Can we make it easier? What if we even made a decorator to make decorators?

Perhaps we could use it catch and log errors:

@decorator
def log_error(func, *args, **kargs):
   try:
       return func(*args, **kargs)
   except Exception, error:
       print "error occurred: %r" % error

@log_error
def blowup():
   raise Exception("blew up")

blowup() # this gets caught and prints the error

Or maybe we'd like to synchronize a function to make it thread-safe:

@decorator
def synchronized(lock, func, *args, **kargs):
    lock.acquire()
    try:
        return func(self, *args, **kargs)
    finally:
        lock.release()

missle_lock = thread.RLock()

@synchronized(missle_lock)
def launch_missiles():
    pass

Or we could even use arguments to do do some object __init__ trickery (something I've had to do when working with wxpython):

@decorator
def inherits_format(method, *args, **kargs):
    self, parent = args[:2]
    self.format = parent.format
    return method(*args, **kargs)

class Child:
    @inherits_format
    def __init__(self, parent):
        pass

class Parent:
    def __init__(self, format):
        self.format = format

format = object()
child  = Child(Parent(format))
assert child.format is format

If you've never worked python decorators, these are few examples of how powerful they are. But you'll probably never want to write your own because it can be a pain. If that's the case, then the decorator decorator is for you! Here's the code for it. It's a little tricky, but all you have to do is import it, slap @decorator before your decorator, make sure you call func(*args, **kargs), and you're all set. This is as easy as decorators can get.

Update: Dave left a comment and notified me that there is another, much more complete, implementation of the same idea at http://www.phyast.pitt.edu/~micheles/python/documentation.html. So, if you want a very complete module, go there. If you want something small and simple to cut and paste into your own code, use the following.

def decorator(decorator_func):
    decorator_expected_arg_count = decorator_func.func_code.co_argcount - 1
    if decorator_expected_arg_count:
        def decorator_maker(*decorator_args):
            assert len(decorator_args) == decorator_expected_arg_count, "%s expects %d args" % (decorator_func.__name__, decorator_expected_arg_count)

            def _decorator(func):
                assert callable(func), "Decorator not given a function.  Did you forget to give %r arguments?" % (decorator_func.__name__)

                def decorated_func(*args, **kargs):
                    full_args = decorator_args + (func,) + args
                    return decorator_func(*full_args, **kargs)

                decorated_func.__name__ = func.__name__
                return decorated_func
            return _decorator
        return decorator_maker
    else:
        def _decorator(func):
            def decorated_func(*args, **kargs):
                return decorator_func(func, *args, **kargs)

            decorated_func.__name__ = func.__name__
            return decorated_func
        return _decorator
    

35 comments:

  1. Hi,

    You might also want to check out Michele Simionato's decorator module. It's more heavyweight but preserves signature, docstrings, and does some other neat tricks.

    ReplyDelete
  2. synchronised decorator example does not work.
    Alain

    ReplyDelete
    Replies
    1. I feel this is among the such a lot vital info for me. And i am satisfied studying your article. However wanna commentary on few general things, The website style is ideal, the articles is truly nice
      Tangki Panel
      Tangki Fiberglass
      Jual Septic Tank

      Delete
  3. Dave, thanks for the heads up. I've never seen that module before, and it looks very complete and nice. It's not the first time I've written something and shared it only to find that someone else has done a better job :).

    ReplyDelete
  4. ليس من السهل الحصول على شركة متميزة ومتخصصه لعملية التنظيف ومكافحة الحشرات ونقل العفش وغيرها من الخدمات فشركتنا رائده في هذا المجاال وجعل منزلك راقي وجميل حيث يتوفر لدى شركتنا عمال فلبيين وغيرهم متميزون وعلى درجه كبيره من الكفاءه والخبره كما ان شركة ركن الشروق يتوفر لديها افضل المعدات واحداثها التي تجعل الشركة متقدمه عن غيرها وجعل امور المنزل من تنظيف او مكافحة او نقل امر سهل وراقي في العمل عزيزي العميل نتمني منك الاسراع الينا وطلب الخدمة التي تريدها وسوف يصل اليك فريق على درجه كبيره من التميز والخبره والسرعه في العمل اتصل بنا ولا تتردد نصلك اينما كنت شركة تسليك مجاري بابها
    شركة مكافحة حشرات بابها
    شركة نقل عفش بابها

    شركة تنظيف خزانات بابها

    ReplyDelete
  5. Idiotic and additionally trusting people at the same time can be an obstacle, on aged and also fully grown folk assembled also will follow a complication. Age and also embryonic concern together. London house painting

    ReplyDelete
  6. عزل اسطح بالدمام لحماية وزيادة عمر الخزان الافتراضي.

    شركه عزل فوم بالدمام عزل فوم

    ReplyDelete
  7. yes, you can watch your Favorite Hallmark Channel series and movies on Hulu activation code roku. Hallmark channel also available on Fubo TV.

    ReplyDelete
  8. Thanks for this wonderfull content and information.hulu.com/activate

    ReplyDelete
  9. Thanks for this wonderfull content and information.hulu.com/activate

    ReplyDelete
  10. I am very interested in this article, Thank you so much for sharing such a article.hulu error code 94

    ReplyDelete
  11. Hulu Error Code 301 is a paid American based video-on-demand subscription service. People pay to activate their accounts but sometimes the wrong Hulu activate device process can cause errors which can result in unwanted delay in watching their favorite TV shows and Movies.

    ReplyDelete
  12. You can easily manage Hulu devices from your Hulu Account.In order to www.hulu.com/activate activation code, you will need to login to your account on the device you’re watching Hulu.

    ReplyDelete
  13. Thanks for sharing the useful information. You have mentioned all essential points for getting Assignment Help easily.hulu.com/activate enter code

    ReplyDelete
  14. Wow, this is really interesting reading. I am glad I found this and got to read it. Great job on this content. I like it. bodrum rodos feribot

    ReplyDelete
  15. Great article with fantastic information found useful and unique content enjoyed reading it thank you, looking forward for next blog.
    typeerror nonetype object is not subscriptable

    ReplyDelete
  16. Hey dear, I'm Dipti very new to the city of joy Juhu Call Girls. I'm from Mumbai by origin south Indian. I'm working here for some MNC company and I'm all alone here in the city of joy. Well, I have heard that there are lots of things to see in the city of joy. Well, dear guys, I'm looking for some secret season. I'm staying in an apartment in Newtown and I'm looking out for some hot secret season so please who are interested contact me. I really want to attain someone who is very decent and understanding. I'm also available for out-call anytime. I really want to keep secrets and please don't share my pics keep things secret. Loyal people, please contact me let's have some fun tonight. Visit Our Web:- http://www.modelnight.net/escort-in-juhu.html

    ReplyDelete
  17. Awesome article…. Really very helpful information for us. Keep blogging. Looking to reading your next post! Thank You for Sharing This Information!
    Online Tutoring Jobs UK

    ReplyDelete
  18. Thanks for this informative article. Nice example of code using decorators, but it's still not very serious. For those who want to master programming at a deeper level and start creating complex things, I recommend looking for posts on the programming language you are interested in on Instagram, in which experienced programmers will tell and show you step by step how to code in your chosen language. Found a lot of similar posts there and many of them had about 49k likes, which makes me think that their authors were using https://soclikes.com/buy-instagram-likes to buy likes.

    ReplyDelete
  19. Welcome to the party of my life here you will learn everything about me. custom patches

    ReplyDelete
  20. We provide a complete consulting service for your essay. Our research tutorial team provides you with quality research materials for you to complete your assignment.

    http://www.dissertationhomework.com

    ReplyDelete
  21. The best HP Printer Support Number with tools for skipping the wait on hold, the current wait time, tools for scheduling a time to talk with a HP repair, reminders when the call center opens, tips and shortcuts from other HP customers who called this number.

    ReplyDelete
  22. We GoAssignmentHelp is a team of experts professional academic writers who provide the best Online Assignment Writing Services with accounting assignment help - marketing assignment helpmarketing assignment help</a in australia for Now you can get professional writing help with all your academic writing easily!

    ReplyDelete
  23. In our online Assignment Help services, we only outsource subject matter professionals for the work. It is both their knowledge and skills in picking the unique words to write the paper that satisfies the needs for help with assignment writing. But, the assignment helpers from our online assignment help services follow the needed process to create the best assignment help paper. Have a glance to understand why our best assignment help service is the right. We can provide you the ideal assignment help service that can help boost your overall scores. Get the right assignment writing service at reasonable rate.

    ReplyDelete
  24. Looking for Homework Help In The USA? I recommend that you should try our academic writing services which you will never regret trying, we offer writing services under all niches and streams within your budget.

    ReplyDelete
  25. Use Microeconomics assignment help if you are unable to create your assignments. Our experts offer high-quality plagiarism-free assignments at a very cheap price. Visit our website to know more about our academic services.

    ReplyDelete

Blog Archive

Google Analytics