Decorators 101 Recap and Review
Congratulations on getting this far. This video is a recap and review of this section. You’ve covered a couple of useful real world examples such as:
- Boilerplate template for creating decorators that you can reuse
- How to time functions
- Using a decorator to build a debugging tool
- Slow down code if you need to
- Making a light-weight plugin architecture to register plugins
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00 Hey! Congratulations on a job well done. You completed Decorators 101. Briefly, I’d like to review Section 3 with you. Section 3 was all about real-world examples, and you created a boilerplate template that you could reuse for the rest of the examples.
00:20 Next, you learned how you could time functions using a decorator.
00:28 Then you created a tool for debugging code that showed you the arguments coming in and out of functions.
00:39 You also learned how you could debug the use of convenience functions by wrapping a decorator around them. Next, if you needed to, you found a way that you could slow down code simply by adding a decorator, and added that to your tool belt.
00:58 And finally, you learned how to make a lightweight plugin architecture to register functions so that you can pull them out and reuse them later. Thanks again for joining me for this tour through Decorators 101.
nerdydodo on March 20, 2019
Great tutorials!! One question, though– After reading the cheat sheet, I found myself not understanding
- the difference between the following two examples
- the use case of example 2
Could anyone here help me understand this better? Thanks!
Example 1
import functools
def name(arg_1, ...):
def decorator_name(func):
@functools.wraps(func)
def wrapper_name(*args, **kwargs):
value = func(*args, **kwargs)
return value
return wrapper_name
return decorator_name
Example2
import functools
def name(_func=None, *, arg_1=val_1, ...):
def decorator_name(func):
@functools.wraps(func)
def wrapper_name(*args, **kwargs):
value = func(*args, **kwargs)
return value
return wrapper_name
if _func is None:
return decorator_name
else:
return decorator_name(_func)
Geir Arne Hjelle RP Team on March 20, 2019
Hi,
the difference is how the decorators are used. The first example decorator must be called with arguments (or at least parentheses):
@name(val1, ...)
def func(...):
...
The second example defines a decorator that can be used both with and without arguments:
@name
def func(...):
...
@name(arg1=val1, ...):
def func(...):
...
There are more details about this in the original article: realpython.com/primer-on-python-decorators/#both-please-but-never-mind-the-bread
Hopefully this makes it a little clearer?
DMonty on March 30, 2019
The video version of this tutorial was much better for me than the written one, which to me just seemed like an exercise to go through. I found the real world examples almost riveting and have them safely saved away!
SamR on April 4, 2019
Very helpful tutorial. Thanks!
Vignesh Anand Krishnan on April 8, 2019
These video materials are so better than the written articles. keep up the good work guys.
Javier Estraviz on April 22, 2019
Great tutorial!! Thanks!
milangowin on May 2, 2019
Awesome tutorial
Vanam on May 9, 2019
Excellent Tutorial and good examples
Fahim on June 24, 2019
Good real world example. Thanks
LJIN Lab on Aug. 10, 2019
Great tutorial, mate
Mark SibleySchreiber on Jan. 9, 2020
I like how the tutorial gets to the essence of the topic.
Sujith M on Jan. 11, 2020
Excellent ! Nuff said.
Patrick Prince on Feb. 15, 2020
Lots of material was covered here. Very useful. Thanks!
Alan ODannel on Feb. 25, 2020
Nice lesson and examples. Looking forward to coming up with creative decorator ideas. I’ll most likely be using the timing and debug decorators in the near future.
rtbjobsml on Feb. 27, 2020
A great intro to decorators
princer800 on March 21, 2020
Thought this was the best explanation of decorators I have seen. I still struggle for how to use them. I would think it would be most useful how to enable and disable the re-definition of a function they basically perform. THat is when debugging - your function would be wrapped to be debugged or timed for example. Then when ready to release the code disable the decorations. Not sure - short of editing the code - how that is done.
fjavanderspek on April 19, 2020
Great course, Chris, thanks!
Howard M Sherman on April 22, 2020
Really liked this class. It was a slow and methodical exposition on a difficult topic. I’ll probably review the class a few more times for it all to sink in. Thanks Chris!!!
tsusadivyago on May 7, 2020
Finally understood the concept of decorators. Thank you for this course.
stefhx on June 14, 2020
I know that this is not just specific to this lesson but I think it would ad some experience that after completing the whole lesson you would get some image in RealPython style and would be send to your profile section devoted to your completed lessons :) Would be better than finishing and nothing happens. ;)
Still.. very good lesson. Thank You :)
Dan Bader RP Team on June 15, 2020
@stefhx: Great to hear you enjoyed the course :) When you complete a course (== all lessons in the course) you’ll automatically receive a completion certificate.
You can access your completion certificates in your list of completed courses but maybe I can make that a bit more visible and easier to find. Thanks for the heads up!
acharya2chn on June 19, 2020
Thank you for this masterpiece! There is so much to understand and learn.
Sammy-Boy on Aug. 29, 2020
It’s super great tutorial. Just one question though what does this !r do in the f-strings? Maybe I missed it on a certain video.
Geir Arne Hjelle RP Team on Aug. 29, 2020
Hi Sammy-Boy.
The !r
format specifier means that repr()
should be used instead of the default str()
when converting the variable.
For string variables it will in practice add single quotes around the string.
Ghani on Oct. 23, 2020
Very informative course; thanks!
Pavel Anni on Jan. 10, 2021
Great tutorial, thanks! Time to think about how to apply it to my current projects :-)
rumeeahmad on Jan. 24, 2021
This is a very in depth and well explained view of decorators. This course has been fleshed out well and examples and use cases are relavent thus making it very simple for begginers to grasp their heads around.
My only reccomedation is to make sure some of the more obscure items are always explained a litle bit more in depth e.g. repr or !r.
Thanks
Mario Henrique Adaniya on Feb. 20, 2021
I’m struggling a little bit in a code that make heavy usage of decorators. The course helped me to understand a little bit more about decorators… but still have tons of doubts.
7ssanM on Oct. 16, 2021
Wonderful summary, easy to grasp, thank you very much Christopher and Real Python team.
aniketbarphe on Nov. 26, 2021
Thank You!
sndselecta on Jan. 7, 2024
Just to better understand. Decorators are basically something referred to as “wrapper functions”. It works as function itself, where a pre-definded function is input as an argument. The object of this wrapper function is to use the inputed function as is without altering its original code, with the additional advantage of imbedding new code before or after and ultimately returning the aggregated output.
Ash D on Jan. 9, 2024
Incredible course - very well thought out and designed. The best RealPython course I’ve done so far (and I’ve done quite a lot!)
The best thing about this course is how much groundwork the teacher laid first, to avoid blowing our brains up when explaining decorators, a topic which most people find quite hard to grasp.
The design of this course shows a huge amount of thought about which aspects of decorators are the sticking point for most newbies, and how convey understanding about the precursor elements that make decorators easier to understand.
I was pleasantly surprised at how relevant and useful the examples were. I liked that the text-entry screen caps are sped up so that we’re not sitting there watching someone type slowly.
Fantastic work, RP team!
Become a Member to join the conversation.
Bradon on March 19, 2019
Great overview of decorators!