Introduction
Setup
Operators
variable
String
Number
Dtype & Comment
Lists
Tuples
Sets
Dictionaries
if else statement
For Loop
While Loop
iterator & iterable
Pre-Built functions
Function
Decorators
Generators
Types of errors
Exceptions Handling
OOP
Regular expression
Create module
OS module
Date module
Random module
Math
Statistics module
Exercise 1
Exercise 2
Exercise 3
Learn Data Structure & Algorithm
Learn Numpy
Learn Pandas
Learn Matplotlib
Learn Seaborn
Learn Statistics
Learn Math
Learn MATLAB
Learn Machine learning
Learn Github
Learn OpenCV
Learn Deep Learning
Learn MySQL
Learn MongoDB
Learn Web scraping
Learn Excel
Learn Power BI
Learn Tableau
Learn Docker
Learn Hadoop
Suppose we have two functions(func1, func2). Func1 will print "I'm from func1" and func2 will print "I'm from
func2".
Suppose these functions are already created. But after creating the function, now we also want that when each
function gives the output that time they also print another thing and that is "This function is very good".
But the problem is we already wrote the code and we don't want to do changes inside the code but we also want
to add something new.
Now what we will do?
In this case, we will use decorators.
In the example, when we called the decorator function and the decorator function takes a parameter which is the function. So we pass the func1 function as a parameter. So first decorator_func will be called and this function will return another function and that is wraper_function. Now, this wrapper function is stored in var1 and var2 variables. So when we call var1, it means we are calling the wrapper function. So after calling var1 wraper_function will run and first it will print "This function is very good", then it will work with the parameter value. Here we pass a function as a parameter. So when the wrapper function will start working with the parameter it will run the function given in the parameter and will print "I form func1".We always have to return wraper_function and take_function.
To use the decorators shortcut, we have to write @decorator_function_name before the main function.
Example:1In example 1 we have two main functions (func1,func2) and a decorator function. Here we wrote @decorator_func before func1 and func2. By doing this we don't have to create a variable and then call the decorator function and also don't need to pass the main function as a parameter. If we just give the call main function then the decorator main function will automatically execute together and will give the output. If we do this then the first decorator function will execute and then the main function will be executed.
Example:2If we want to use parameters and pass a value of the parameter to the main function(func1, func2) then we have to write *args and **kwargs in wrapper function and take_function.