Decorators - Advanced Python Tutorial #2

NeuralNine · Beginner ·🛠️ AI Tools & Apps ·5y ago

Key Takeaways

This video tutorial demonstrates the use of decorators in Python, including adding functionality to functions, logging, authentication, and modifying behavior without changing source code, utilizing tools like the logging module and time module.

Full Transcript

[Music] what is going on guys welcome back to the python tutorial series for advanced programmers in today's video we're going to talk about decorators so let's get right into it all right so the basic idea behind decorators is that they add a certain functionality to a function or they surround the function they wrap the function with an additional functionality and i'm going to show you that by using uh just a function in a function first and then i'm going to show you how it's actually done in python so let's say we have a function which is called i don't know my decorator and this my decorator function is used to decorate other functions so what we do is we pass a function we say my deck creator we give to that function another function and then inside of that decorator we specify a so-called wrapper function so we have this wrapper function here and what this function does is it has some functionality and then it calls the function so let's say here we're just going to print a message like i am decorating your function like that and then what we're going to do is we're going to call the function now the interesting part here is now we're not just executing this we're actually returning the raptor function so as a result of that we're not just calling the decorator and passing the function and then we have the decorated output we're actually passing we're actually returning another function that calls the initial function but with decorated code with additional code so how would we do that we would essentially say let's say we have something like hello world here and we just print hello world like that uh now this helloworld function wanna decorate it so what we can do here is we can say my decorator uh and we can pass hello world without calling it we're just passing it as a reference and what we get here when we execute that function is a return value now of course we can save that return value first into a function or something but we can also directly call it if we want to like that so essentially what we're doing here is we're we're calling this my decorator we're passing the hello world function and what we get as a result is the wrapper function that has the additional functionality and also is executing the initial function and then we call this function that we get as a return value so what we're going to do now is we're going to execute this by just saying python main p1 you can see i am decorating your function hello world now of course we can also swap these positions here by just saying uh we're going to have the function first and then we're going to still decorate it this is also possibility so in this case uh we would have hello world first and then i'm decorating the function so there's also a possibility this is the basic idea behind decorators but this is not how it's done in python now when i say this is not how you do it in python i don't mean the whole thing here i mean this particular line of code we're not calling the function passing the function and then calling the result there's a more elegant way in python and this is just using annotations so we can actually delete this line here and on top of this function i can just say at my decorator so by doing that i'm decorating hello world with the function of my decorator so this structure here is the same but now instead of passing it and calling it or calling the wrapper result we're just saying okay add my decorator and then we can actually call hello world itself and it's already decorated so this is enough as you can see oh sorry as you can see we're already uh getting the hint that this is the actual wrapper function so if i run this right now we get the same results so we see hello world i'm decorating a function again we can also change this um by swapping the order and you can see that the order is swapped so this is the exact same thing but we have some limitations here now they're not real limitations because we can easily fix them but right now the way we have it right now uh it doesn't work perfectly for example if i have some parameters here it doesn't work because the wrapper doesn't have the same parameters so um if i have hello world or let's let's just say hello uh i don't know person we need to say hello to a specific person and what we want to do is want to say hello and then the person's name like that a very simple function still uh but we cannot just decorate it like that because the wrapper here doesn't have that function so if i say hello mic and i'm trying to execute this right now you're going to see that we get a type error because that rapper doesn't take positional arguments but we give one to it because hello takes arguments but uh the decorator the wrapper function here does not take arguments now we could easily fix that by just adding an argument here but the point is that a decorator is not necessarily just for one function if i have a decorator that decor that decorates multiple uh different functions we don't want it to be limited to one specific signature we don't just want to say this decorator is specifically for the function hello because then i could just add the code of the decorator to the function hello we want this decorator to be universally applicable and because of that we have to add the arcs and quarkx keywords here so arguments and keyword arguments i mean we don't need to add the keyword arguments necessarily but if you do it like that you're on the safe side because whatever we pass here we can also pass here by simply passing exactly that so this is a safe a safe thing here because obviously if i pass any arguments by by doing that by calling the function here mike i'm passing it to the arguments and i'm passing the exact same arguments to the function so essentially i'm just passing it right to hello again so it's it's not a big deal and if i do it like that you're going to see that it's going to work i say hello mic however we now have another problem let's say it's not print but it's return i want to return a string if i want to return a string here i say return i'm not going to return it by just calling i need to get the return value from the wrapper as well and what we need to do here essentially to make this work is we need to return the value of the wrapper so we need to say um actually we need to just say come on return return function so we can just return the function and then it's the same thing but we're going to see that this has another problem here as well uh let's just see first of all if it works um i mean we would probably have to print the result then print there you go i am decorating your function hello mike so it works but what if i want to have that print afterwards so if i want to have it like that if i want to first return the value or first execute the function i'm not going to first return the value that's not possible but if i first want to execute a function and then want to print or do the wrapper stuff it doesn't have to be a print it can be whatever you want it to be but i want to execute the decorating functionality afterwards after the function call but i still want to have the return value i cannot do it like that what i need to do then is i need to execute the the function call i need to store it in a variable so for example return value is going to be whatever the function returns to us and then we're going to say return after we did the code return the return value like that and then it would also work like that and uh actually did it work the way you want it to work i'm not sure because actually we would want to have hello mic first oh obviously that's not possible in this particular case because we're returning and we're printing down here uh but if i would have something like print and return so if it would have something like print uh the same string hello mike actually not my person if we would do it like that we would see a difference here because if i do it now we get hello mike i'm decorating a function hello mic and if i'm not doing it like that if i'm directly returning or if i'm if i'm doing it like that i'm going to first get i'm decorating your function dense two times hello mic [Applause] as you can see now let's go ahead and look at two very simple uh practical applications because up until now this was quite abstract so let's actually go ahead and delete all of this and now what we're going to do is we're going to have the first practical example practical example number one is going to be logging now this is not how you would actually do logging in python in the intermediate tutorial series we already talked about the logging module and so on but for the sake of simplicity i'm just going to open up a file and write to it in a very uh in a very simple way i'm not going to use the logging module here um but we're going to see why maybe using decorators here might be a little bit more comfortable than actually logging every time in a manual manner so let's say we have this function or decorator locked we're just going to call it locked we add a function or pass a function to it and what we do then is we have the wrapper function def wrapper and we pass arguments and keyword arguments so that it's definitely working and then what we're going to do is we're going to say value or return value whatever it's just the function called on the arguments on the keyword arguments so that it's compatible with all different types of functions and now we can decorate actually so now we can go ahead and log whatever is happening and we can do this by saying with open log file dot txt in appending plus as f and what i'm going to do here is i'm just going to say let's first of all get some some values here so let's say fname equals function dot underscore underscore name underscore underscore and this would give us the function name and then we can say f dot write and we can formulate an f string here we can say function name uh returned value value like that and of course we can also use the same thing to print this so we don't need to necessarily uh to necessarily just write it into the file we can also say print and we can print the same thing here like that so what we do after that of course is in case there's a return value we're going to return it otherwise we're going to just return none or anything like that uh and then we're going to say return the wrapper here and we're done with the deck creator and now i can have a very simple function here uh first of all do i have any errors here no i don't have any errors so let's say we have a function add and we have just x and y here and what we're doing is we're just returning x plus y a very simple function here uh and now i can first of all show you what happens without decorators so we're just going to say print at 10 plus 20 in a very simple way here so first of all we have an invalid syntax oh obviously because we have one star for the arcs and two for the keyword arcs so this should work yeah there you go you get 30 as a result and now we're going to add the locked decorator here so we're going to say add locked and we're going to see that there is a difference here and you can see add return value 30 and we should also be able to see it here so let's open this in split mode here and you can see at return value 30 and of course if i go ahead now and execute this again and again and again and again um probably we need to close this and open this up again but you can see oh maybe we should add maybe we should add a new line here as well so we should say backslash and in the end so that we always log in a new line and you can do whatever you want you can also use the actual logging module you can use the timestamp and so on but this is one practical example of why decorators might might be useful so let us look at a second example here and this example is going to be timing so let's say you have a function you want to know how long it took to execute so we're going to have the second practical example your practical example 2 is going to be timing functions so for this we're going to import time and we're going to have the decorator called time uh timed like that i'm going to pass a function here we're going to have the wrapper here and this wrapper what it's going to do again it's going to take uh arguments we're going to take keyword arguments and uh we're going to basically do the same thing but before we do anything we're going to have the first time stamp so we're going to say okay before equals time dot time so we're getting getting the the current time right now and then we're starting with the whole execution so we say value equals um function on the arcs and keyword arcs like that and then we say after equals time of time so what we're doing is the whole function call the actual function call of the past function is happening in this particular line and before that line and after that line we have a timestamp so we can now go ahead and calculate the difference so we can say okay print uh oh i don't know let's say actually we can get the file name as well here so let's say fname equals oh not finally function name sorry function dot underscore underscore name and we can say i don't know fname took and now we say just after minus before seconds to execute like that and we can now go ahead and do i don't know what we can just define a random function my function here and the only thing this function is going to do it's going to have a result starting at one and then we're going to say for x or actually not x maybe let's see for i in range uh x uh we're going to say result times equals i it's a very simple thing and then we can return that result return result and the basic idea here is that we just want to have something that takes uh takes the time takes some time to be calculated uh do i have a problem there oh yeah i need to return the wrapper i'm not sure if that's the problem though yeah seems to be the problem um and then what i'm doing here is i'm just saying oh and we need the value as well actually we don't need the value we can just call the function here so i can i guess i can get rid of that um yeah like that so we can just call the function here in this case unless we're returning something actually maybe we should return it though yeah that's that's let's do a return here so let's say value equals that and then after the print we're just going to return the value like that um and then we're going to add the tag here but first of all again let's just see what happens if we just say print my function uh ten thousand let's go for a hundred thousand let's see if this kind of works here we get zero why do we get zero we have oh because we start at zero that's not good let's do it like that and it takes some time maybe takes too much time so let's go with one less zero here and you can get a result here so if i now go ahead and time this we're going to get a timing as well so we're going to get timed here and uh i think the message somewhere up there maybe we should not print it maybe we should just execute it without the print so let's get rid of that and let's get rid of that so that we only see the execution time you can see it took 0.025 seconds now if i change this to a 9 here maybe it's going to take longer so um hopefully not too long yeah there you go 5.6 seconds to execute so this is one other practical example you could just go ahead and have this time decorator whenever you know whenever you want to know how fast the function executes and maybe some other values as well you can just add at times above it and then you have the execution time so that's it for this we hope you enjoyed i hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you very much for watching see you next video and bye [Music] you

Original Description

In this video we talk about decorators in Python. ◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾ 📚 Programming Books & Merch 📚 💻 The Algorithm Bible Book: https://www.neuralnine.com/books/ 🐍 The Python Bible Book: https://www.neuralnine.com/books/ 👕 Programming Merch: https://www.neuralnine.com/shop 💻 Exclusive Content 💻 👥 Patreon: https://www.patreon.com/neuralnine 🌐 Social Media & Contact 🌐 📱 Website: https://www.neuralnine.com/ 📷 Instagram: https://www.instagram.com/neuralnine 🐦 Twitter: https://twitter.com/neuralnine 🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/ 📁 GitHub: https://github.com/NeuralNine 🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from NeuralNine · NeuralNine · 0 of 60

← Previous Next →
1 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video tutorial covers the basics of decorators in Python, including how to add functionality to functions, log function calls, and measure execution time, with practical examples and code snippets.

Key Takeaways
  1. Create a decorator function that takes a function as an argument
  2. Define a wrapper function inside the decorator that calls the original function with the provided arguments
  3. Log the function call and return value using the logging module or a file
  4. Return the result of the original function
  5. Use the decorator to add logging to an existing function
  6. Implement a locked decorator to restrict access to certain functions
  7. Use the time module to get the current time and measure execution time of functions
💡 Decorators provide a powerful way to modify and extend the behavior of functions in Python without changing their source code, making them a valuable tool for developers.

Related Reads

📰
I Accidentally Stumbled Into an AI Side Hustle — Here's Everything I've Learned
Learn how to monetize AI tools by reselling AI API access, a legit business model that doesn't require coding skills
Dev.to AI
📰
How I Made $700 in 2 Weeks Selling AI Automation Templates
Learn how to create and sell AI automation templates to generate passive income, with a real-life example of making $700 in 2 weeks
Dev.to AI
📰
MCP Server Tutorial: Build Your Own AI Tools in 30 Minutes
Learn to build a custom MCP server with AI tools in 30 minutes using TypeScript and NeuroLink SDK
Dev.to · NeuroLink AI
📰
AI didn’t replace the work for me. It moved the stress to a different place.
AI automates initial work phases but shifts stress to evaluation and refinement, making it essential to adapt and manage new challenges
Reddit r/artificial
Up next
How AI Is Transforming Analytics in Tableau Cloud & Server
Salesforce Product Center
Watch →