In this article we will understand, What are decorators? How decorators work? How to create your own decorators in Python To understand decorators in Python we should know what are functions in Python. In Python, everything is object. Functions in Python are first-class objects which means that they can be referenced by a variable, added in the lists, passed as arguments to another function etc. Functions can be referred by variables Basic function in Python: We can make another variable to refer to function as shown below: say_hello2 say_hello With the above statement and are pointing to the same function definition, and execution of both will produce same result say_hello2 say_hello Functions can be passed as arguments to another function As functions are objects in python they can also be passed as parameters to another function. In the above example, we have passed function as an argument to function. is referenced by variable. Inside , is called which references and it prints “Hi”. say_hi say_hello say_hi say_hi_func say_hello say_hi_func say_hi Functions can be defined inside another function In the above example, function has been defined inside function. It is valid in python. When we call function , function gets defined inside function and it is called inside function. say_hi say_hello say_hello say_hi say_hello say_hello() If we try to call outside function, Python will give error because does not exists outside of function. say_hi say_hello say_hi say_hello Functions can return references to another function In the above example, function returns a reference for function. Returned function reference is assigned to Thus also starts pointing to function. say_hello say_hi say_hi_func. say_hi_func say_hi Another example with function arguments Variable is accessed even outside the function because function was defined inside say_hello function so it can access all the variables of function. This is called as closure. hello_var say_hello say_hi say_hello With the understanding of functions, now let’s understand decorators. Decorators Decorators are callable objects which are used to modify functions or classes. Callable objects are objects which accepts some arguments and returns some objects. and are examples of callable objects in Python. functions classes Function decorators are functions which accepts function references as arguments and adds a wrapper around them and returns the function with the wrapper as a new function. Let’s see this with an example: In the above example, is the decorator function which accepts , a function object, as an argument. It defines a which calls but it also executes some code of its own. decorator_func some_func wrapper_func some_func This is returned by our decorator function and it’s stored in variable. Thus now say_hello refers to which has some extra code apart from calling the function which was passed as argument. Thus in another way we can say that, decorator function modified our function and added some extra lines of code in it. This is what decorators are. The output is the modified function with extra statements wrapper_func say_hello wrapper_func say_hello say_hello print Python syntax for decorator @decorator_funcdef say_hell():print 'Hello' The above statements is equivalent to def say_hello():print 'Hello' say_hello = deocrator_func(say_hello) Here, will added some code on top of the function and will return the modified function or the wrapper function. decorator_func say_hello Functions with arguments and decorators Consider the below example for function with parameters and decorators: Here, we have defined function with two arguments and . Inner function of must take exactly the same number of argument as function. say_hello @decorator_func decorator_func i.e. wrapper_func say_hello Here. @ validates if the passed parameters are not empty and none, if they are, it calls function with default arguments. decorators_func say_hello Passing parameter to a decorator function To pass parameter to a decorator function we write a wrapper function to the decorator which then defines the decorator function inside itself. Example: Other articles: An Extensive Guide To Progressive Web Applications Let’s get this ‘this’ once and for all Service Workers Service Workers implementation Zip in Python lambda, map and filter in Python Concatenating two lists in Python List comprehensions in Python is how hackers start their afternoons. We’re a part of the family. We are now and happy to opportunities. Hacker Noon @AMI accepting submissions discuss advertising & sponsorship To learn more, , , or simply, read our about page like/message us on Facebook tweet/DM @HackerNoon. If you enjoyed this story, we recommend reading our and . Until next time, don’t take the realities of the world for granted! latest tech stories trending tech stories