Python Decorators
A decorator takes in a function, adds some functionality and returns it. In this article, you will learn how you can create a decorator and why you should use it.

Table of Contents:
- What are decorators in Python?
- Prerequisites for learning Decorators
- Getting back to Decorators
- Decorating Functions with Parameters
- Chaining Decorators in Python
What are the decorators in Python?
Python has an exciting image called the decorating to include the code.
This is also called metaprogramming as part of the program is trying to change the other parts of the program when it is released.
Prerequisites for learning Decorators:
To understand the beauty, you must first know the basic elements of Python.
You do not need to enjoy the fact that everything in Python (Yes! Even in class) is the thing. The name we make is based only on the identification of the item. The task is not an exception, they are also something (with attributes). Different types of varieties may be the same for the purpose of the project.
Below is an example.
def first(msg):
print(msg)
first("Hello")
second = first
second("Hello")
When the code is implemented, the first two and the second give the same. Here, the first and second names are the object.
Things to start now to get a store.
Activities can be referred to other arguments.
When you use functions such as map, filter and lower Python, then you'll know this before.
Such tasks will take other activities such as debates as well as higher tasks. Below are examples of these activities.
def inc(x):
return x + 1
def dec(x):
return x - 1
def operate(func, x):
result = func(x)
return result
We used it as a function.
>>> operate(inc,3)
4
>>> operate(dec,3)
2
On the other hand, the activity can be for other activities.
def is_called():
def is_returned():
print("Hello")
return is_returned
new = is_called()
#Outputs "Hello"
new()
Here, the is_returned () function is defined inside the outside, whenever we call is_called ().
Finally, you should know about closing the Python.
Getting back to Decorators:
Functions and methods are unacceptable, as they can be called.
In fact, any item that specifies __ is the particular __call method () is called 'invocable'. Therefore, basic sensitivity is a practice that is implemented and returns to practice.
Basically, the work takes one, adding a bit of work and return.
If you fall into the keys,
def make_pretty(func):
def inner():
print("I got decorated")
func()
return inner
def ordinary():
print("I am ordinary")
In the example above, make_pretty () is cosmetic. Work steps.
>>> ordinary()
I am ordinary
>>> # let's decorate this ordinary function
>>> pretty = make_pretty(ordinary)
>>> pretty()
I got decorated
I am ordinary
Normal work () is decorated in the beautiful name.
We can see that the decorator adds a few new jobs and original jobs. This is like gift gifts. The globe works for a container. The beauty of gifts (gifts of gifts) do not change. But now, it looks beautiful (since it is not adorned).
Usually, we decorate and reform activities such as,
This generic building is because Python has a word to facilitate this.
We can use @ symbol with the name of the cosmetics name, and we can attach a description of the cosmetics. For example,
This is just a sweeter taste for cosmetics applications.
Decorating Functions with Parameters:
The simplest of the mirror simply work with jobs that do not have boundaries. What if we have a job in the following territories?
def divide(a, b):
return a/b
This consists of two limits, a and b. We know it's wrong if we kill as much as 0.
>>> divide(2,5)
0.4
>>> divide(2,0)
Traceback (most recent call last):
...
ZeroDivisionError: division by zero
Now we make cosmetics to make sure if this leads to a mistake.
def smart_divide(func):
def inner(a,b):
print("I am going to divide",a,"and",b)
if b == 0:
print("Whoops! cannot divide")
return
return func(a,b)
return inner
@smart_divide
def divide(a,b):
return a/b
The inspector will find out that the borders of the interior () of the same plateau of the same beauty decorate the work. With this in mind, we can do all in the cosmetics working with each of the borders.
In Python, the magic works like (args, ** kwargs). In the same way, terrorist terrorists mentioned in the word are the kwargs of the essential words. For example, such decorations.
Decorating Functions with Parameters:
Some decorations can be connected to Python.
In this way, the beauty of the dress can be several different (or similar) cosmetics. I will just put the decor in the high tower.
def star(func):
def inner(*args, **kwargs):
print("*" * 30)
func(*args, **kwargs)
print("*" * 30)
return inner
def percent(func):
def inner(*args, **kwargs):
print("%" * 30)
func(*args, **kwargs)
print("%" * 30)
return inner
@star
@percent
def printer(msg):
print(msg)
printer("Hello")
This will give you a break.
******************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Hello
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
******************************
In the above verses,
@star
@percent
def printer(msg):
print(msg)
equivalent
def printer(msg):
print(msg)
printer = star(percent(printer))
Charging orders give you orders. If you have to apply as,
@percent
@star
def printer(msg):
print(msg)
The application will be implemented as,
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
******************************
Hello
******************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
To getting expect level training for Python training in your Location – Python training in Chennai | Python training institute in Chennai | Python Course institute in Chennai | Python training in Bangalore | Python training institute in Bangalore | Python training in Electronic City | Python training in Jaya Nagar | Python training in Pune | Python training institute in Pune | Python training in OMR | Python training in Velachery | Python training in Tambaram | Python training in Annanagar | Python training in Marathahalli | Python training in Btm | Python Online Training | Online Certification Course | Python Course in Chennai | Python interview questions and answers | Python tutorials | Python training in Indira Nagar | Python Course in Btm | Python Course in Marathahalli | Python training institute in Marathahalli | Python course institute in Btm | Python course in Electronic City | Python online training in Chennai | Python online training in Bangalore
Comments
Post a Comment