#100DaysOfCode in Python Transcripts
Chapter: Days 22-24: Decorators
Lecture: More abstraction: stacking decorators

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Next up, let's talk about how you can stack decorators. And I've found an image of Russian dolls, which might clarify how this "nesting" works.
0:13 So let's define another decorator that shows the args and keyword args being passed in, and we're going to stack that
0:20 together with the timeit decorator.
0:38 Alright, let's modify generate report from the last video to take some arguments. And now, let me show you how you can stack
0:50 the two decorators we've defined so far, and note that the order matters. So I put timeit as the outer decorator,
0:58 because I want to time the whole operation, including the use of the print args decorator. Let's now call it, but first let's
1:07 give it some parameters, so let me quickly find a dictionary of some keyword arguments. And now, all should come together,
1:19 because when I call generate report with some args and some keyword args, look at that. We see two decorators in action.
1:29 First a timer, starting the timer, doing stuff, ending the timer, and printing how long it took, and then we see the inner decorator,
1:36 print args, printing the args and the keyword args. And here you see that decorators can become pretty powerful, because each decorator is doing
1:45 a specific task, which can be applied to multiple functions, yet it's all abstracted in their definition.
1:52 And, yes, this is how you can stack decorators.


Talk Python's Mastodon Michael Kennedy's Mastodon