call() decorator in Python Last Updated : 29 Dec, 2019 Comments Improve Suggest changes Like Article Like Report Python Decorators are important features of the language that allow a programmer to modify the behavior of a class. These features are added functionally to the existing code. This is a type of metaprogramming when the program is modified at compile time. The decorators can be used to inject modified code in functions or classes. The decorators allow the program to be modified to add any code that has a special role to play. Decorators are called before the definition of the function you have decorated. The use of decorators could be explained with the following example. Suppose we write a program to "Decorate" a function using another function. the code goes like: Python3 1== # Code to explain Decorators def decorating(function): def item(): print("The function was decorated.") function() return item def my_function(): print("This is my function.") my_function() decorate = decorating(my_function) decorate() Output This is my function. The function was decorated. This is my function. Firstly, "This is my function" appears because of the function call my_function(). The second set of output was because of the Decorating function. The same thing can also be done by using decorators. The following code explains that. Note that the decorating statement is defined above the function to be decorated. Python3 1== # Code to implement the usage # of decorators def decorating(function): def item(): print("The function was decorated.") function() return item # using the "@" sign to signify # that a decorator is used. @decorating def my_function(): print("This is my function.") # Driver's Code my_function() Output The function was decorated. This is my function. The call() decorator The call() decorator is used in place of the helper functions. In python, or in any other languages, we use helper functions for three major motives: To identify the purpose of the method. The helper function is removed as soon as its job is completed. And The purpose of helper function matches with that of decorator function. The following example will illustrate the significance of the call decorator method. In this example, we would be building a list of the doubles of the first "n" numbers, using a helper function. The code is as follows: Python3 1== # Helper function to build a # list of numbers def list_of_numbers(n): element = [] for i in range(n): element.append(i * 2) return element list_of_numbers = list_of_numbers(6) # Output command print(len(list_of_numbers), list_of_numbers[2]) Output 6, 4 The above code could also be written using the call() decorator: Python3 1== # Defining the decorator function def call(*argv, **kwargs): def call_fn(function): return function(*argv, **kwargs) return call_fn # Using the decorator function @call(6) def list_of_numbers(n): element = [] for i in range(n): element.append(i * 2) return element # Output command print(len(list_of_numbers), list_of_numbers[2]) Output 6, 4 As it is observed, that the output is same as before, this means that the call() decorator works almost exactly like helper functions. Comment More infoAdvertise with us Next Article call() decorator in Python M muskangupta13 Follow Improve Article Tags : Technical Scripter Python Python Decorators Practice Tags : python Similar Reads Decorators in Python In Python, decorators are a powerful and flexible way to modify or extend the behavior of functions or methods, without changing their actual code. A decorator is essentially a function that takes another function as an argument and returns a new function with enhanced functionality. Decorators are 10 min read Conditional Decorators in Python In Python, decorators are functions or classes that wrap around a function as a wrapper by taking a function as input and returning out a callable. They allow the creation of reusable building code blocks that can either change or extend behavior of other functions. Conditional Decorators Given a co 2 min read Closures And Decorators In Python Closures and decorators are powerful features in Python that allow for more advanced and flexible code patterns. Understanding these concepts can greatly enhance your ability to write clean, efficient, and reusable code.Why Python decorators rather than closures?Python decorators are preferred over 3 min read Chain Multiple Decorators in Python In this article, we will try to understand the basic concept behind how to make function decorators and chain them together we will also try to see Python decorator examples. What is Decorator In Python?A decorator is a function that can take a function as an argument and extend its functionality an 2 min read Nested Decorators in Python Everything in Python is an object. Even function is a type of object in Python. Decorators are a special type of function which return a wrapper function. They are considered very powerful in Python and are used to modify the behaviour of a function temporarily without changing its actual value. Nes 2 min read Debugging decorators in Python Decorators in Python are really a very powerful feature. If you are a web developer and you have used the Django framework or even some other development frameworks you would have already come across decorators. For an overview decorators are wrapper functions that wrap an existing function or a met 3 min read __call__ in Python Python has a set of built-in methods and __call__ is one of them. The __call__ method enables Python programmers to write classes where the instances behave like functions and can be called like a function. When this method is defined, calling an object (obj(arg1, arg2)) automatically triggers obj._ 4 min read Dispatch Decorator in Python Decorators are a very powerful and useful tool in Python since it allows programmers to modify the behavior of function or class. Decorators allow us to wrap another function in order to extend the behavior of the wrapped function, without permanently modifying it. Example: Python3 # defining a deco 2 min read callable() in Python In this article, we will see how to check if an object is callable in Python. In general, a callable is something that can be called. This built-in method in Python checks and returns True if the object passed appears to be callable, but may not be, otherwise False. Python callable()In Python, calla 3 min read classmethod() in Python The classmethod() is an inbuilt function in Python, which returns a class method for a given function. This means that classmethod() is a built-in Python function that transforms a regular method into a class method. When a method is defined using the @classmethod decorator (which internally calls c 8 min read Like