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:



Output:
 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,


Output:
  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:



Output:
  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.

22 comments:

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.

Bhanu Sree said...


This is an amazing blog, thank you so much for sharing such valuable information with us.
Python Course in Hyderabad
Python Institute in Hyderabad
Python Online Training in Hyderabad
Python Training in Hyderabad
Python Training
Python Online Training

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! :-)

keerthana said...

The information that you have shared is really useful for everyone. wonderful article. Nicely written and great info.Thanks to share the more information's.PHP Training in Chennai

PHP Online Training in Chennai

Machine Learning Training in Chennai

iOT Training in Chennai

Blockchain Training in Chennai

Open Stack Training in Chennai

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 this.you 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

dhinesh said...

An overwhelming web journal I visit this blog, it's unfathomably amazing. Unusually, in this present blog's substance made inspiration driving truth and reasonable. The substance of data is enlightening.


Full Stack Course Chennai
Full Stack Training in Bangalore

Full Stack Course in Bangalore

Full Stack Training in Hyderabad

Full Stack Course in Hyderabad

Full Stack Training

Full Stack Course

Full Stack Online Training

Full Stack Online Course




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

ajith said...

thank you for the information


angular js course in chennai

angular course in chennai

angular js online course in chennai

angular js course in bangalore

angular js course in hyderabad

angular js course in coimbatore

angular js course

angular js online course


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

lkrasilnikovaludmila1976 said...

health and wellnesskeerthi suresh nude sara ali khan boobs disha patani hot pics nude indian actress anushka sharma nude tamannasex kajal aggarwal nude kriti kharbanda nude anveshi jain porn alia bhatt naked

rstrainings said...


I am really happy to say it’s an interesting post to read . I learn new information from your article , you are doing a great job . Keep it up

Devops Training in Hyderabad

Hadoop Training in Hyderabad

Python Training in Hyderabad

Tableau Training in Hyderabad

Selenium Training in Hyderabad

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

https://superdachpups.com/ said...

Its always nice and i feel honored reading your Blog . Your Blogs always based on all basic concepts which anyone can understand so easily and relate to Pleasure to read your Blogs Sir.



golden retriever puppies for sale near me
dachshund puppies for sale near me
golden retriever puppies for sale in pa

Linkfeeder said...

Professional tennis player as well as mental health expert mynews today news india news live today breaking news news live today breaking news

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

Morshedul Arefin said...

Effective Post. Tech Blog

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