Saturday, July 13, 2013

Using Python decorators for registering callbacks

In my previous post we talked about Python decorators and an intuitive way of remembering how decorators with arguments work.

The following code snippet had triggered the whole chain of thought:

Now that we know what decorators with arguments do, which is essentially - calling the decorator factory with the argument, using the argument to make logical branching in the decorator wrapper and returning the wrapper, we can now try to understand the above code. We first move on to the decorator route's source code found here to see it's implementation. Keep the source code opened in a different tab, as we will refer to it in the later sections.

This code deviates a bit from what we think about how decorators are used - decorators wrap the functionality of a target function with certain pre and post actions, like so:

 Decorator calling func
 In the function
 Decorator called func

But there isn't any call to the target function index in the route function definition. Instead, this code snippet throws light on another functionality of decorators - registering callbacks.

Callbacks are registered functions which are stored in some container data structure (mostly hashes with key as function name and value as function references). For example,

  calling func1 with arg 1
  calling func2 with arg 2

They are useful in building a map of functions and depending on specific user input, the hashes are looked up and the corresponding key's value - which is a function reference is called.

Let us revisit our main snippet. The decorator @app.route('&#47') call leads to:

which in turn leads to the method self.add_url_rule:

  app.add_url_rule('/', 'index', index)

says to Flask - when a user requests for &#47, call the function mapped against the index key, which is the index function.

So that's about it - write a function, declare a decorator above it, that decorator will take the function, store the reference of it in a hash, and will use it whenever an event occurs connected to the key against which the function reference was stored.

Now to paint the whole picture here's a self-contained minimal example of using decorators for registering callbacks:

  This is the main page.
  This is the next page.

Hope you enjoyed going through this post. Do let me know your insights and views in the comments section.


latesttechnologyblogs said...
This comment has been removed by the author.
dan said...

excellent explanation - the best I have seen so far !!!
well done

dk9mbs said...

great explanation. Now I understand the creation of Flask like app classes woth decorator support.

Twisted_Code said...

Until recently, I didn't even know what decorators were, other than that they involved the at-symbol followed by an identifier. I had seen something similar in Java as part of Minecraft Forge mods, but didn't really know what was going on. I just knew it was something to do with event handling. This all changed when I started looking at a Python-based discord bot that registers callbacks in this way. After I learned what decorators were, I realized the "at identifier" notation was passing the relevant functions in order to return the "hooked" function just like this. Thanks for contributing to my understanding of that process. Your explanation was very helpful in confirming what I suspected! :-)

rocky said...

Thanks a lot for sharing kind of information. Your article provide such a great information with good knowledge.You make me happy for sharing, in this post some special information.thanks.
python training in bangalore

python training in hyderabad

python online training

python training

python flask training

python flask online training

python training in coimbatore

pavi said...

I feel really happy to have seen your webpage.I am feeling grateful to read gave a nice information for us.please updating more stuff content...keep up!!

Data Science Training In Chennai

Data Science Online Training In Chennai

Data Science Training In Bangalore

Data Science Training In Hyderabad

Data Science Training In Coimbatore

Data Science Training

Data Science Online Training

sharath said...

Hi, This is nice article you shared great information i have read it thanks for giving such a wonderful Blog for reader.
Java Training in Chennai

Java Training in Bangalore

Java Training in Hyderabad

Java Training
Java Training in Coimbatore

Rohit said...

It was reaaly wonderful reading your article. # BOOST Your GOOGLE RANKING.It’s Your Time To Be On #1st Page
Our Motive is not just to create links but to get them indexed as will
Increase Domain Authority (DA).We’re on a mission to increase DA PA of your domain
High Quality Backlink Building Service
1000 Backlink at cheapest
50 High Quality Backlinks for just 50 INR
2000 Backlink at cheapest
5000 Backlink at cheapest

Linkfeeder said...

Annabelle loves to write and has been doing so for many years.Cheapest and fastest Backlink Indexing Best GPL Store TECKUM IS ALL ABOUT TECH NEWS AND MOBILE REVIEWS

The Vesign said...

Oppo launch First foldable smartphone .Oppo has teased the company’s first foldable phone.The image of the device is only shown from the spine side and we get a glimpse of the phone’s profile, revealing what appears to be a triple camera setup Article
Click Here For website

MSD Arefin said...

Awesome Post. Thanks For This Article

haseeb said...

Note that Office2019 DO NOT support activate via simple copy/paste plist license file which is the simplest way to activate Office 2016.MS Office 2019 Crack

haseeb said...

This article shows you how to download and install the full version of Microsoft Office 2010 for free on PC. Follow the direct download link .MS Office 2010 Activator Key