call() decorator in Python

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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()

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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()

chevron_right


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:

  1. To identify the purpose of the method.
  2. The helper function is removed as soon as its job is completed. And
  3. 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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])

chevron_right


Output

6, 4

The above code could also be written using the call() decorator:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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])

chevron_right


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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.