Decorators - Intermediate Python Programming p.18
Key Takeaways
This video tutorial covers the concept of decorators in Python programming, including their definition, usage, and application in various scenarios, such as adding wrapping to functions and methods, and using them with object-oriented programming.
Full Transcript
Hey everybody, what's going on? Welcome to part 18 of our intermediate Python tutorial series. In this video, what we're going to be talking about is decorators and what they do. So decorators, as they sound, add decoration to your function. You can kind of think of them almost like they wrap your function around. Um, so just as a quick example, you don't have to write this out. This is just an example of say a contact page on a Flask application. Uh, in web development with both Flask and Django, decorators are pretty common, at least for me, that was where I first really had to use decorators. Uh, and basically what this does is, you know, you write your function that does something. It returns this template, this contact template, and then the beauty of using a web framework is you can just use this decorator. And using this decorator, it wraps this function and basically adds some code to it that says, okay, when the user goes and visits this URL, we do the thing that the this function, we return this template that's the contact template. Okay, so just a really quick example of where where decorators are used in the wild. Now, let's actually talk about decorators and and uh see how they work truly with example. So let's say let's say you've got a function and this function is for a a new GPU and this GPU is uh we return a new Tesla Tesla P100. Nice. It's a nice GPU. Christmas is coming. Anybody want to send me one of these? Just let me know. I'll give you my address. Next we've got uh we could do print uh new GPU. Uh, we can save and run that. Uh, oh, we didn't run it. Okay, there we go. Don't forget your parenthesis. Uh, so we run it. Sure enough, it's a new GPU. Now, since you guys are are uh sending it to me for Christmas, maybe you want to wrap the GPU. So, you might have a a new function and we'll say define add wrapping. When you wrap something, you're you're wrapping, right? You're wrapping something. So you're going to pass whatever the item is that you want to wrap into this wrapping function. This will be our decorator function. And now we're going to say we're going to define wrapped item. And what this is going to do is return a um a wrapped up box of something. And then we'll do uh dot format the string version of whatever the item is. So uh so we return that and then now at the at for the actual add wrapping function we're going to return wrapped item. So now what we can do is at um new GPU we can wrap it. So we can say boom add wrapping and that will decorate add our wrapping paper to our new Tesla P100 GPU. So we can now run literally new GPU. Nothing changes. We've just added that wrapping with the decorator. Um so this is basically our decoration. We can run that and now what we get is actually a wrapped up box of a new Tesla P100 GPU. Thank you guys. I love your wrapping. So, um what's nice is with this wrapping functionality when you have um separate functions and also you can you can use this on like class methods and stuff like that. Uh you can use it to all kinds of things. So, right you could say define uh new bicycle. I don't need one of these. You don't need to send me one return. But seriously, on that GPU, let me know a new bicycle. Uh and we can wrap that as well. So we can add uh add wrapping to that and then return uh print rather new bicycle and you get a wrapped up box of a new bicycle. Okay, so you can use wrapping to do that kind of stuff. Now what happens if you added wrapping to the wrapping? So you're one of these people who like does boxes inside of boxes to fool people what it is. So, for example, with his GPU, the box will be, you know, maybe about like this big or something. But you're one of these people that puts it in a huge box and then puts another box over it and stuff like that. So, so maybe you would do you can do this. You can add wrapping. So, we'll just add another wrapping or you could even have uh you could put like another you could create a new decorator function that's a bow or something like that. Something different than just adding wrapping twice. But anyway, we can do the same thing. Now it's a wrapped up box of a wrapped up box of a new Tesla P100 GPU. But you could have created rather a decorator function that was you know add wrapping add bow and then have the box or add bow add wrapping the box whatever. Okay. So you can do that kind of double um the double wrapping. Now one thing that probably won't matter to most of you but especially when you're using this possibly with object-oriented programming and so on. Uh what if we said print new GPU name? So we're just going to grab the name of what should be just like the function name basically. Let's see what it says though. Instead, what we're returned is wrapped item because it's it's actually a wrapped item function now because what it returns is this. And in many cases, you're not going to want that. You're going to want that to actually return new GPU. Like so like if we did this right it's going to say the name is actually it's a new GPU function right and so depending on what you want maybe you actually want it to say this because it is a wrapped item function as well it's a wrapped item so that's true but maybe you actually would rather this be um the the original function name what you can do is you could come to the top here and from funk tools tools import wraps and that gives us that wraps is a decorator. So what we can say is at wraps item and then basically we changed nothing else. We'll save and run that and it's the original function now. So it doesn't that name object or the the name method rather won't won't get overwritten um if you don't want it to. Again there going to be times where well it is a wrapped item right? So so you you you are going to want to override it but then there's going to be other times where but it's really actually a new GPU. I don't want to overwrite that. You could do that and there's probably plenty of times where you don't even care. So do whatever you want there. Now what we're going to do is is basically in case this function and another function. It's going to be define add wrapping with style. And we'll pass the style. And then we'll tab this over. And now we're going to say a and then we'll do some formatting. Uh and then over here we'll say style comma string item. So you might say a nicely or a badly or whatever. It's there return wrapped item. And then we come down here and actually we're going to return add [Music] wrapping. And now what we can say is we can add wrapping um let's say uh beautifully beautifully. Awesome. So print new GPU. We'll save that. Run that. Uh add wrapping. Oh. Oh, okay. Add wrapping with style. Whoops. Add wrapping with style. Run that again. And now what you get is a beautifully wrapped up box of a new Tesla P100 GPU. And then you could say like this now um horribly. And now it's a horribly wrapped up box of a beautifully wrapped up box of a new Tesla P100 GPU. Okay, so I think that's enough on decorators, but there's obviously a lot of really cool things that you can do and it kind of gives you a lot more dynamic functionality to your functions. Uh, so it's actually pretty nice and also methods too. But, um, I do like the added functionality basically of the do not repeat yourself that you can do with this and still the dynamicness because you can pass parameters. Obviously, you don't have to just use strings. You can use variables and all kinds of cool stuff. Uh, this is just meant to be a super simple example. But, uh, yeah, pretty cool. So, if you have questions, comments, concerns, something cool with decorators you want to show, let us know below. Otherwise, I'll see you in another tutorial.
Original Description
Welcome to part 18 of the intermediate Python programming tutorial series. In this tutorial, we are going to be discussing decorators, which can be used to sort of wrap or quite literally add some "decoration" to functions and methods.
https://pythonprogramming.net
https://twitter.com/sentdex
https://www.facebook.com/pythonprogramming.net/
https://plus.google.com/+sentdex
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
🎓
Tutor Explanation
DeepCamp AI