Decorators in Python - How to use it?

Pallavi Dhotre

2 months ago

Decorators in Python | insideAIML
Table of Content
Introduction
Decorators
  • How to use decorators in python
  • Function decorators in python
Summary

Introduction 

          Want to know about python decorators, with examples? Take a deeper look at this article. In this article, we are going to discuss decorators, python decorators example, types of decorators in python, decorator function in python, and function decorators. But before that, we should know what are decorators in python? 

Decorators

          Design patterns through which we can add functionality to an existing instance without changing the structure are called python decorators. Python is mostly used for writing less code to achieve more functionality. In python, we can save our time and energy by using special keywords starting with ‘@’ in the coding. These special keywords are written just before the class definition or functions are called decorators. There are two types of decorators in python. The first is the function decorator and the second is the class decorator.  Each decorator has a specific meaning. There are built-in decorators like @classmethod, @staticmethod, @property in python. 

How to use decorators in python?

          Let’s talk about @property.  We need to define at least three blocks of code for each property.
  • Set function
  • Get function
  • Property definition using ‘property’ keyword
@property
def place(self):
    return self._place
One of the decorators provided by python is @property. It exposes a function as a property. Since we marked the ‘place’ function, it became a property (calling code can now call x.place to get value).
For the setter function, we use @propertyName.setter. This property name 
@place.setter
def place(self, placeName):
    if placeName not in ['Rome','France','Los Angeles ']:
        self._place = 'Bangkok'
        print('Not a Destination')
    else:
        self._place = placeName
        print('Your Destination is here')
As we observed, decorators enhance the usefulness (the same function is now a property) of the function as well as provide shortcuts to new functionality (no need to write property statement).
Our Destination class now looks like this:
#python decorator examples
class Destination:
    def __init__(self, place):
        self.place=place

    @property
    def place(self):
        return self._place

    @place.setter
    def place(self, placeName):
        if placeName not in ['Rome','France','Los Angeles ']:
            self._place = 'Bangkok'
            print('Not a Destination')
        else:
            self._place = placeName
            print('Your Destination is here')
d = Destination('Rome')
In the given example we can see that in class definition @property decorator is used after the constructor and before using the setter method. Then we have used the setter method to set the value to the variable. If we write the @property decorator after the setter method then the program will through an error. 

Function Decorators in Python

          References of functions are passed as an argument to another function and adding a wrapper around them to return the wrapper as a new function is called function decorators. 

Summary

          In this article, we discussed python decorators, python decorators example, types of decorators in python, how to use python decorators, and function decorators in python. Design patterns through which we can add functionality to an existing instance without changing the structure are called python decorators. There are two types of decorators in python. The first is the function decorator and the second is the class decorator.  Each decorator has a specific meaning. There are built-in decorators like @classmethod, @staticmethod, @property in python. We hope you enjoyed the article. If you have any related queries, feel free to ask in the comment section below. 
    
Enjoyed reading this blog? Then why not share it with others. Help us make this AI community stronger. 
To learn more about such concepts related to Artificial Intelligence, visit our blog page.
You can also ask direct queries related to Artificial Intelligence, Deep Learning, Data Science and Machine Learning on our live discussion forum.
Keep Learning. Keep Growing.
              
   

Submit Review