Locked learning resources

You must own this product to watch this lesson.

Locked learning resources

You must own this product to watch this lesson.

Decorating Functions With Arguments

This lesson is from the Real Python video course by Christopher Bailey.

00:00 What if you need to pass arguments into your decorated functions? Let me show you that next. I’ll have you keep using decorators.py. Back here in the REPL, from your module decorators, import do_twice. To decorate a new function, start with @do_twice, then define like normal.

00:27 But this time, let’s say we’re defining greet() with the argument name, and then it will use an f-string, with the curly braces, to print that name.

00:40 So here’s greet. It looks like it’s been properly decorated. In this case, you pass in "World", so it will say "Hello World"

00:52 uh oh, it looks like we found an error. The wrapper_do_twice() function that’s part of the decorator that you’ve imported takes zero positional arguments, and you gave it one.

01:04 Could you modify your decorator @do_twice so that it could take an argument? Sure. This may get a little messy though. So back inside decorators.py, you could create an argument named one_arg and pass it into the function—in this case, both function calls—and save. In order for you to reuse this decorator,

01:35 you’ll need to exit the REPL and then re-import it.

01:43 Let’s start my REPL again.

01:54 And redefine your function, greet(), printing f"Hello {name}". Okay, so here’s greet. Pass into greet(), "World" when you call it—great! That worked fine.

02:14 What about if you create using the same decorator @do_twice your old friend the function say_whee()? Oh, don’t forget the exclamation point.

02:28 Okay. So here’s say_whee—looks good. Let me have you call it. So now it’s saying it’s missing one required positional argument, but in the case of greet()

02:46 give it an argument—that works fine. I’m going to go back in time just one moment and show you something. When calling say_whee() in my REPL bpython, it shows that say_whee() takes one argument, which isn’t true. When it was defined, it accepted no arguments.

03:10 That’s very interesting. That’s something the decorator is doing. And since this decorator is set up to take one argument—that’s the name of the argument—it’s applying that to this function also.

03:25 Is it possible back here in our decorator to be flexible? To accept one, two, three—multiple arguments, and also potentially except none? Sure! There’s a standardized way to do that. Let’s rewrite this.

03:46 You’re going to add *args and **kwargswhich stands for arguments and keyword arguments—to your decorator.

04:00 And whatever arguments or keyword arguments that are passed through our wrapper here will get passed when those functions are called. So, let’s save. Okay. So the trick again, back here in our REPL, is I do need to exit and start my REPL again.

04:22 Okay. Now, if you import, from decorators import do_twice, can you define @do_twice decorating greet()?

04:36 Here’s greet. It looks good. Notice how, as I type this, it shows greet with *args and **kwargs available.

04:46 Let’s greet the world. Great! That worked fine. And what if you apply @do_twice—the decorator—to a simpler function, like say_whee(), that takes no arguments—will that work?

05:03 So, here it is. Yep! It looks good. And again, if I freeze for a second, you’ll see that say_whee(), which by definition didn’t take any arguments, shows *args and **kwargs. Not that that function, which the way it was defined, can actually accept any. The decorator would accept arguments, and then pass them into our function, and that’s where it would fail.

05:32 And now call it without any arguments. So now, functions that have no arguments can be decorated with @do_twice, and functions that do have arguments passed in can be decorated with that same decorator, because you have added *args and **kwargs and are passing those into our functions.

05:59 That’s the flexibility of using them. Great! Next, let’s return values from decorated functions.

You must own this product to join the conversation.