Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

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


Course Slides (PDF)

446.9 KB

Bradon on March 19, 2019

Great overview of decorators!

nerdydodo on March 20, 2019

Great tutorials!! One question, though– After reading the cheat sheet, I found myself not understanding

  1. the difference between the following two examples
  2. 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):
        def wrapper_name(*args, **kwargs):
            value = func(*args, **kwargs)
            return value
        return wrapper_name
    return decorator_name


import functools

def name(_func=None, *, arg_1=val_1, ...):
    def decorator_name(func):
        def wrapper_name(*args, **kwargs):
            value = func(*args, **kwargs)
            return value
        return wrapper_name

    if _func is None:
        return decorator_name
        return decorator_name(_func)

Geir Arne Hjelle RP Team on March 20, 2019


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:

def func(...):
@name(arg1=val1, ...):
def func(...):

There are more details about this in the original article:

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.


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!

Become a Member to join the conversation.