Python decorators are usually created with function, see another related post, but this post also shows an example on how to create decorators with class.

In [1]: def greet(name):
   ...:     print("Hi %s" % name)
   ...:

In [2]: greet("Andy")
Hi Andy

In [3]: class Polite(object):
   ...:     def __init__(self, func):
   ...:         self.func = func
   ...:     def __call__(self, *args, **kwargs):
   ...:         self.func(*args, **kwargs)
   ...:         print("Nice to meet you")
   ...:

In [4]: @Polite
   ...: def greet(name):
   ...:     print("Hi %s" % name)
   ...:

In [5]: greet("Andy")
Hi Andy
Nice to meet you
BACK TO TOP
Post tagged with: python, aspect oriented programming