Source

Python decorators finally demystified

Hi there guys! I hope all of you are fine and doing well. Recently I was hanging out on a python related IRC where I got a request from someone to write an article on decorators in Python. It is perhaps one of the most difficult concept to grasp. So as usual without wasting anytime let get on with it.

Everything in python is an object (Functions too!):

First of all let’s understand functions in python:

def hi(name="yasoob"):
    return "hi "+name

print hi()
#output: 'hi yasoob'

#We can even assign a function to a variable like
greet = hi
#We are not using parentheses here because we are not calling the function hi
#instead we are just putting it into the greet variable. Let's try to run this 

print greet()
#output: 'hi yasoob'

#lets see what happens if we delete the old hi function!
del hi
print hi()
#outputs: NameError

print greet()
#outputs: 'hi yasoob'

Defining functions within functions:

So those are the basics when it comes to functions. Lets take your knowledge one step further! In Python we can define functions inside other functions. You might be wondering what sorcery is this! Let me explain it with an example.

def hi(name="yasoob"):
    print "now you are inside the hi() function"

    def greet():
        return "now you are in the greet() function"

    def welcome():
        return "now you are in the welcome() function"

    print greet()
    print welcome()
    print "now you are back in the hi() function"           

hi()
#output:now you are inside the hi() function
#       now you are in the greet() function
#       now you are in the welcome() function
#       now you are back in the hi() function

# This shows that whenever you call hi(), greet() and welcome()
# are also called. However the greet() and welcome() functions
# are not available outsite the hi() function e.g:

greet()
#outputs: NameError: name 'greet' is not defined

So now we know that we can define functions in other functions. In simpler words we can make nested functions. Now you need to learn one more thing that functions can return functions too.

Returning functions from within functions:

It is not necessary to execute a function within another function, we can return it as an output as well. Let’s take a look at it with an example!

def hi(name="yasoob"):
    def greet():
        return "now you are in the greet() function"

    def welcome():
        return "now you are in the welcome() function"

    if name == "yasoob":
        return greet
    else:
        return welcome

a = hi()
print a
#outputs: <function greet at 0x7f2143c01500>

#This clearly shows that `a` now points to the greet() function in hi()
#Now try this

print a()
#outputs: now you are in the greet() function

Just take a look at the code again! In the if/else clause we are returning greet and welcome, not greet() and welcome(). Why is that? It is so because when you put parentheses around it the function gets executed whereas if you don’t put parenthesis around it then it can be passed around and can be assigned to other variables without executing it. Did you get it? Let me explain it a little bit in more detail. When we write a = hi() hi() gets executed and because the name is yasoob by default, the function greet is returned. If we change the statement to a = hi(name = "ali") then the welcome function will be returned. We can also do print hi()() which outputs now you are in the greet() function. I hope you have not fainted by now. Was it difficult? No? I guess you are in the mood to finally learn about decorators! Lets continue our talk and move forward.

Giving a function as an argument to another function:

Lets take a look at an example:

def hi():
    return "hi yasoob!"

def doSomethingBeforeHi(func):
    print "I am doing some  boring work before executing hi()"
    print func()

doSomethingBeforeHi(hi)
#outputs:I am doing some  boring work before executing hi()
#        hi yasoob!

Congratulations! You have all of the knowledge to entitle you as a decorator wrangler! Oh wait, I still haven’t told you what decorators really are. Here is a short definition:

decorators let you execute code before and after the function they decorate

Writing your first decorator:

You have already written your first decorator! Do you know when? In the last example we actually made a decorator! Lets modify the previous decorator and make a little bit more usable program:

def a_new_decorator(a_func):

    def wrapTheFunction():
        print "I am doing some  boring work before executing a_func()"

        a_func()

        print "I am doing some boring work after executing a_func()"

    return wrapTheFunction

def a_function_requiring_decoration():
    print "I am the function which needs some decoration to remove my foul smell"

a_function_requiring_decoration()
#outputs: "I am the function which needs some decoration to remove my foul smell"

a_function_requiring_decoration = a_new_decorator(a_function_requiring_decoration)
#now a_function_requiring_decoration is wrapped by wrapTheFunction() 

a_function_requiring_decoration()
#outputs:I am doing some  boring work before executing a_function_requiring_decoration()
#        I am the function which needs some decoration to remove my foul smell
#        I am doing some boring work after executing a_function_requiring_decoration()

Did you get it? We just applied the previously learned principles. This is exactly what the decorators do in python! They wrap a function and modify its behaviour in one way or the another. Now you might be wondering that we did not use the @ anywhere in our code? That is just a short way of making up a decorated function. Here is how we could have run the previous code sample using @.

@a_new_decorator
def a_function_requiring_decoration():
    print "I am the function which needs some decoration to remove my foul smell"

a_function_requiring_decoration()
#outputs: I am doing some  boring work before executing a_function_requiring_decoration()
#         I am the function which needs some decoration to remove my foul smell
#         I am doing some boring work after executing a_function_requiring_decoration()

#the @a_new_decorator is just a short way of saying:
a_function_requiring_decoration = a_new_decorator(a_function_requiring_decoration)

Decorators Demystified:

I hope you now have a basic understanding of how decorators work in Python. They are not something to be afraid of at all. In addition, we can chain two or more than two decorators! For example:

def bread(func):
    def wrapper():
        print "</''''''\>"
        func()
        print "<\______/>"
    return wrapper

def ingredients(func):
    def wrapper():
        print "#tomatoes#"
        func()
        print "~salad~"
    return wrapper

def sandwich(food="--ham--"):
    print food

sandwich()
#outputs: --ham--
sandwich = bread(ingredients(sandwich))
sandwich()
#outputs:
#</''''''\>
# #tomatoes#
# --ham--
# ~salad~
#<\______/>

In the following example e-satis really does a great job demonstrating how decorators are used. We can run the previous example as so:

@bread
@ingredients
def sandwich(food="--ham--"):
    print food

sandwich()
#outputs:
#</''''''\>
# #tomatoes#
# --ham--
# ~salad~
#<\______/>

The order in which you use decorators matters and can change the whole behaviour of your decorated function if they are not executed in the intended order. Suppose you write this:

@ingredients
@bread
def sandwich(food="--ham--"):
    print food

sandwich()
#outputs:
##tomatoes#
#</''''''\>
# --ham--
#<\______/>
# ~salad~

Nobody wants a sandwich with tomato on the top and salad on the bottom. This demonstrates why you should not mix up the arrangement of the decorators; otherwise you’ll pay the price. In our case we paid the price by ruining our beautiful sandwich.

I hope you learned something useful from this post! If there’s any demand I will cover some more gotchas and features of decorators in the future. Please share your views in the comments below. If you have any suggestions, then please do tell me about them.

You can also follow me on facebook, twitter, or you can send me an email. The links to my social media accounts are at the bottom of the post. Farewell, and don’t forget to subscribe to my blog, tweet, and share this post on facebook.

Newsletter

×

If you liked what you read then I am sure you will enjoy a newsletter of the content I create. I send it out every other month. It contains new stuff that I make, links I find interesting on the web, and occasional discount coupons for my book. Join the 5000+ other people who receive my newsletter:

I send out the newsletter once every other month. No spam, I promise + you can unsubscribe at anytime

✍️ Comments

Brian

Love it!!! Never really understood them before…though that’s probably because i skimmed over that portion of ‘Learning Python by O’Reilly’…the language there just made it too boring…your post clarifies that topic though these seem to be very simple examples…i’d like to see some more complex stuff done with decorators…

Yasoob
In reply to Brian

I will cover complex stuff in a future post :)

Christian

Thank you! This realy helps me alot :)

Sajith Dilshan

Thank you very much. This post has demystified decorators indeed.

Yasoob
In reply to Sajith Dilshan

I am happy that you liked it :)

khuram

Thanks for posting such a staff…… was helpfull…. :)

dummyaccnt

Thank you so much Yasoob. Flask uses decorators for routing and I was having a very hard understanding decorators until I read your post. Do you have any posts regarding Flask?

Yasoob
In reply to dummyaccnt

No currently I don’t have any posts on Flask but I will surely write some in near future.

Steve Newcomb

Many thanks! It takes great care to write so little and say so much:

a_function_requiring_decoration = a_new_decorator( a_function_requiring_decoration)

Better for me than any example!

tevin joseph

Good one :) It would be nice if you could write an article on class decorators.

Narayanreddy

thank you very much could you post some real time projects on python for beginers in detail manner

FoureB

Really fantastic explanation. Thank you! I couldn’t understand any of the other explanations online until I came to your webpage. A million thanks for helping me land my first Python Developer job!

Yasoob
In reply to FoureB

That’s fantastic! :) Really good to know

Marouane El-anbri

clarified decorators’s ambiguity in Mind . Thank you very much

Nidhi Durai Pandian

The sandwich example was too good!! thank you very much

Say something

Send me an email when someone comments on this post.

Thank you!

Your comment has been submitted and will be published once it has been approved. 😊

OK