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 refer to our video player troubleshooting guide for assistance.

Syntactic Sugar

In this lesson, you’ll see a simpler and more common way to apply decorators to a function using the @ symbol. You’ll see how to go from this:

Python
def my_decorator(func):
    def wrapper():
        print("Something is happening before the function is called.")
        func()
        print("Something is happening after the function is called.")
    return wrapper

def say_whee():
    print("Whee!")

say_whee = my_decorator(say_whee)

To writing decorators this way:

Python
def my_decorator(func):
    def wrapper():
        print("Something is happening before the function is called.")
        func()
        print("Something is happening after the function is called.")
    return wrapper

@my_decorator
def say_whee():
    print("Whee!")

00:00 It’s a little clunky how the simple decorators were working, as far as applying them to the function. Let me show you a bit of syntactic sugar that you can apply when using decorators. I want to start off repeating a bit of what I showed you last time.

00:15 Here are the two functions created like last time. To apply the decorator, you took the existing function’s name and then reassigned it to my_decorator() with the function you want to apply it to as an argument inside of it.

00:37 But there is another way to do this that’s a little prettier and you’ll see way more often inside of Python code. Let’s take a look. Starting with my_decorator(), passing in func as the argument, defining my wrapper() function, and the wrapper()

01:03 prints "Something is happening before the function is called." It calls the function, and prints "Something is happening after the function is called." Ending the statement by returning the wrapper function.

01:19 To apply a decorator, there’s a different way that it can be done. This is that syntactic sugar that I’ve mentioned a couple of times now. You start with the at symbol (@).

01:31 You use the @ symbol and then the function name for the decorator. So @—in this case—my_decorator. Then just below it, you would define your function.

01:43 So any new function that you want wrapped around with this decorator, you simply add @my_decorator before beginning to define that function.

01:56 Okay.

02:01 Now if you call say_whee(),

02:06 you can see that it’s already been wrapped by @my_decorator. If you enter in just say_whee here, it shows the same function that is wrapped around the local my_decorator() function. It’s calling to that.

02:23 So, it’s a little prettier looking. A little easier to identify in your code where your decoration’s happening, compared to this style here. Defining it, defining your function, and then reassigning it.

02:42 This is nice! Going forward, you’ll be using this syntax for the rest of the tutorial. Next up, let’s reuse some decorators.

TechBanerg on March 21, 2019

Chris, you are barely audible, could increase the volume of this lecture.

Become a Member to join the conversation.