Python Decorators Decoded!

Decorators in Python is not a fairly new concept. It was introduced way back and we all must have(if only a few times!) used decorators somewhere.

In my early days of coding, I came across lots of questions about number of topics in Python. The one which kept coming every time was “decorators”(Not the quotes, hah!).Apart from the fact that only a few understand Decorators completely, I wonder what keeps a novice developer from using/developing one of its own. The answer is simple enough. It is lack of proper understanding.

Ohkay, lots being said and discussed.Now lets get started with its definition and usage. To start with,

A Decorator is a function that returns another function, with some added or transformed capabilities.

>>> def decorator(function):
def wrapper(parameter):
return something
return wrapper

So what exactly is happening here?

Let me explain you with a very simple example.
Suppose we have a function named say_hello(name) that takes in a name and prints out ”hello name”
 To be specific in this case I have passed in name as rahul, so it prints hello rahul.

>>> def say_hello(name):
return “hello {}”.format(name)
>>>print say_hello(‘rahul’)
‘hello rahul’

Now what if we wanted to have this message inside {{ }} tags??? Decorators come to the rescue here .See how.

>>> def curly(func):
def wrapper(name):
return ‘{{‘ + func(name) + ‘}}’
return wrapper
>>> @curly
def say_hello(name):
return “hello {}”.format(name)
>>> print say_hello(“rahul”)
{{hello rahul}}

Here we first define the decorator curly.
It takes a function as a parameter and encloses a wrapper(name) function which takes name as its parameter.
This name is nothing but the parameter ‘name’ passed in the original function.
The wrapper then returns the string after enclosing it within {{ }} to the decorator curly

In the next line, we call the decorator using the python syntax @curly
we then define our function say_hello(name) as we did in the code snippet above.

And then we just print out the result to the see magic!
Simple??Yes !!Offcourse.

Now you could argue that we could have done this using the same say_hello function as well.Right?? Well yes, surely we can but what if we had to perform same task over a lot of other functions or many other types of input strings?

Okay, so lets show you my way of using it while I was working on a Django Project.Have a look at the code below :-)

>>> print test({“name”:”rahul”})
{u’name’: u’rahul’}
>>> print test([“rahul”,”lakhanpal”])
[u’rahul’, u’lakhanpal']

So you must have by far sensed, what I been trying to do with the above code.
I have taken in some specific values and converted them to unicode.

Well http://thecodeship.com/patterns/guide-to-python-function-decorators/is another link to a very great tutorial which I personally liked a lot.Do check that out!

Let me know for any queries, feedback or corrections!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.