Every programming language has its own culture

(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com, or follow me on Twitter.

It is interesting to note that this article about decorators in Python does not use the “word” closure, even though that would be the obvious way of talking about these, were we discussing a language like Javascript, where discussion of closures has been central to the culture since Doug Crockford first discussed them back in 2001.

Only in the comments does someone ask about closures:

So, decorators are an application of closures, i.e. closures applied to functions? Could you explain the relationship between decorators/closures?

As to my understanding, your “surround_with”-function is a closure (not a decorator), while your decorator function “currency” is a closure as well, which accepts a function as an argument (“f” in this case).

Therefore, the only difference is that the “currency” -decorator receives a function as an argument (decorator –> closure with function as an argument? ) – as functions are first-class objects, there is no real difference at all?

To which the author responds:

Decorators don’t have to be functions; you can use a class (or any callable) as a decorator too. But yes, when a decorator function returns a function other than the original one (it can just return the original one, of course), the reference to the “wrapped” function is held in a closure.

Also, the word “polymorphic” does not appear in the article, even though function-factories that accept functions are a type of polymorphism.

Post external references

  1. 1
    http://www.jeffknupp.com/blog/2013/11/29/improve-your-python-decorators-explained/
Source