Decorators by Python

Posted in python by pedro | Monday, June 2nd, 2008 at 1:38 pm

Lately, I’m using Django. So I also learn Python. And I found that python is very clean and nice language.

As we know Python allows that function can also be a parameter to another function. So code similar to this is ok.

def decorator(fun):
print fun.__name__
def my_fun():
print “hello world”

decorator(my_fun)

But Python provides some syntactic sugar so the code above is the same as this code:

def decorator(fun):
print fun.__name__
@decorator
def my_fun():
print “hello world”

So why it is so important, because I’m Java programmer and this allows me in elegant way to use something similar to Aspect. For example I can write cache decorator and apply it to every calculation intensive function.

class cache:
def __init__(self, function):
self.function = function
self.cache = {}
def __call__(self, *args):
try:
return self.cache[args]
except KeyError:
self.cache[args] = self.function(*args)
return self.cache[args]

Now for our propose I use a non optimal fibonacci recursive function.

def fib(n):
if n > 1:
return fib( n -1 ) + fib ( n -2 )
return 1

And simply measured that using enough large fibonacci number.

from time import gmtime, strftime
print “started %s” % strftime(“%a, %d %b %Y %H:%M:%S”, gmtime())
print fib(35)
print “ended %s” % strftime(“%a, %d %b %Y %H:%M:%S”, gmtime())

With and without @cache decorator. The output is

With @cache:

started Mon, 02 Jun 2008 20:32:41
14930352
ended Mon, 02 Jun 2008 20:32:41

and without:

started Mon, 02 Jun 2008 20:33:09
14930352
ended Mon, 02 Jun 2008 20:33:19

The conclusion is that it is great possibility to use such a class/function as I’m used to when using aspect with java.

Leave a Reply

about me

My name is Sebastian Pietrowski. I've finished Warsaw University as Master degree. I started my journey with Java 1.1 with Thread and JDBC programing in 1998 as I worked for merlin.pl. In 1999 I've passed Java Programer Certificate for Java 1.2, and was solution architect of merlin.pl infrastructure when we was moving from pl/sql to J2EE. It was great performance optimization with 10 times more req/sec than in requirements and 85 times faster as original solution.

Currently I work as Expert Software Development Java at F.Hoffmann-La Roche. The company was founded in 1896 and today, Roche employs over 80.000 people. After work I'm involved in activities related to Scala/Lift, Ruby/Rails/Merb, Python/Django. This is because I try to be pragmatic also I'm focused on application performance and tuning with success in my daily work.

My Yoda's motto: Do, or do not. There is no try.