Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

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
Download

Course Slides (PDF)

446.9 KB
Avatar image for Bradon

Bradon on March 19, 2019

Great overview of decorators!

Avatar image for nerdydodo

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):
        @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)
Avatar image for Geir Arne Hjelle

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?

Avatar image for DMonty

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!

Avatar image for SamR

SamR on April 4, 2019

Very helpful tutorial. Thanks!

Avatar image for Vignesh Anand Krishnan

Vignesh Anand Krishnan on April 8, 2019

These video materials are so better than the written articles. keep up the good work guys.

Avatar image for Javier Estraviz

Javier Estraviz on April 22, 2019

Great tutorial!! Thanks!

Avatar image for milangowin

milangowin on May 2, 2019

Awesome tutorial

Avatar image for Vanam

Vanam on May 9, 2019

Excellent Tutorial and good examples

Avatar image for Fahim

Fahim on June 24, 2019

Good real world example. Thanks

Avatar image for LJIN Lab

LJIN Lab on Aug. 10, 2019

Great tutorial, mate

Avatar image for Mark SibleySchreiber

Mark SibleySchreiber on Jan. 9, 2020

I like how the tutorial gets to the essence of the topic.

Avatar image for Sujith M

Sujith M on Jan. 11, 2020

Excellent ! Nuff said.

Avatar image for Patrick Prince

Patrick Prince on Feb. 15, 2020

Lots of material was covered here. Very useful. Thanks!

Avatar image for Alan ODannel

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.

Avatar image for rtbjobsml

rtbjobsml on Feb. 27, 2020

A great intro to decorators

Avatar image for princer800

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.

Avatar image for fjavanderspek

fjavanderspek on April 19, 2020

Great course, Chris, thanks!

Avatar image for Howard M Sherman

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

Avatar image for tsusadivyago

tsusadivyago on May 7, 2020

Finally understood the concept of decorators. Thank you for this course.

Avatar image for stefhx

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

Avatar image for Dan Bader

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!

Avatar image for acharya2chn

acharya2chn on June 19, 2020

Thank you for this masterpiece! There is so much to understand and learn.

Avatar image for Sammy-Boy

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.

Avatar image for Geir Arne Hjelle

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.

Avatar image for Ghani

Ghani on Oct. 23, 2020

Very informative course; thanks!

Avatar image for Pavel Anni

Pavel Anni on Jan. 10, 2021

Great tutorial, thanks! Time to think about how to apply it to my current projects :-)

Avatar image for rumeeahmad

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

Avatar image for Mario Henrique Adaniya

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.

Avatar image for 7ssanM

7ssanM on Oct. 16, 2021

Wonderful summary, easy to grasp, thank you very much Christopher and Real Python team.

Avatar image for aniketbarphe

aniketbarphe on Nov. 26, 2021

Thank You!

Avatar image for sndselecta

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.

Avatar image for Ash D

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.