While recently reading this article on Python decorators, it gave as example the technique of memoization as an application of Python decorators. While I have a working understanding of Python decorators I wish to know of more such effective use cases for decorators, and how do you typically use them in your everyday code.
-
possible duplicate of [What is memoization good for and is it really all that helpful?](http://stackoverflow.com/questions/3242597/what-is-memoization-good-for-and-is-it-really-all-that-helpful) – detly Apr 12 '12 at 12:19
-
I realise that duplicate is not Python-specific, but I think it answers your question. – detly Apr 12 '12 at 12:21
-
1@detly Actually no, My question isn't at all about memoization, I merely wanted to illustrate with an example. My question is specifically about python Decorators and their Good use cases like memoization – subiet Apr 12 '12 at 12:24
-
Ah, you're right. But I can't withdraw the close vote dammit. – detly Apr 12 '12 at 13:27
4 Answers
There are a number of built-in decorators that can be useful, such as classmethod
, property
, staticmethod
, and functools.wrap
. It is often handy to write a decorator that logs usage of functions for debugging purposes. There are lots of example decorators on this Python wiki page, though I think at least some of these are more aimed at showing off how flexible Python is than actually providing useful functionality.

- 3,191
- 1
- 23
- 39
Since Python 3 supports type annotations, decorators can be used as a way to check.
def check(f):
def _f(x):
if type(x) != f.__annotations__['x']:
raise TypeError(type(x))
val = f(x)
if 'return' in f.__annotations__ and f.__annotations__['return'] != type(val):
raise TypeError(type(val))
return val
return _f
@check
def f(x: int) -> int:
return x * 2
>>> f(2)
4
>>> f('x')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in _f
TypeError: <class 'str'>

- 6,737
- 1
- 26
- 37
You can look at effective use cases for python decorators in the Django web framework auth
docs. Clear examples (I personally use on an everyday basis) include the usage of decorators for restricting views to authenticated users, restricting access depending on specific user permissions, etc.

- 14,760
- 10
- 76
- 102
-
1For ease of others @Josvic answer might be referring to [this](https://docs.djangoproject.com/en/dev/topics/http/decorators/) among others. – subiet Apr 12 '12 at 12:19
-
@subiet Exactly. I just referred to specific ones I really find useful :) – Joseph Victor Zammit Apr 12 '12 at 12:20
This is a practical example. Take a look at fibonacci's series sum with and without memo.
from functools import wraps
def memo(func):
cache = {}
@wraps(func)
def wrap(*args):
if args not in cache:
cache[args] = func(*args)
return cache[args]
return wrap
def fib(i):
if i < 2: return 1
return fib(i-1) + fib(i-2)
@memo
def fib_memo(i):
if i < 2: return 1
return fib_memo(i-1) + fib_memo(i-2)
And now test the speed difference!
>>> print fib(200)
...
>>> print fib_memo(200)
...

- 2,529
- 1
- 17
- 25
-
This is the exact example I mention in the link in the question. The usefulness of decorators in this scenario prompted me to seek further use cases like this :) – subiet Apr 12 '12 at 12:21
-
@subiet I didn't read the article, classical example, it almost seems like an exact copy paste :). Anyways, when you work with algorithms, nodes and graphs memo is used to remove what is called recursive complexity (especially useful in partitioning and searches). – luke14free Apr 12 '12 at 12:24