How to Pass a List of Arguments to a Wrapper Function in Python

Suppose you have created a function which perform addition and subtraction of two numbers. Now, you want the product of these numbers. For this, you need to modify this code. After writing code, your client asked you to calculate the execution time of this program also. Now, you will have to change the code again. It is a quite difficult process when your code is lengthy and complex. There should be some method by which we can update our code without changing it again and again. To avoid this, we use function wrappers. In this tutorial, we will learn what is a wrapper function, how you can create them and how to pass a list of arguments to a wrapper function in python.

If you want to learn more about Python Programming, visit Python Programming Tutorials.

What are Wrapper functions?

Wrappers allows us to extend the behavior of any function or a class without actually changing it. They serve as an interface to adapt to existing code, saving you from repeatedly modifying your code. In python, decorator serve as a wrapper function and allow us to wrap a function. Simply, wrapper function is a function wrapped inside another function

Lets first see a simple program to understand how wrappers are created. Suppose you want to modify a function named as “original_function” in the example below. Create a decorator which will wrap an original function with additional functionalities. Inside this decorator, we’ll define the wrapper function.

# Function you want to wrap
def original_function():
  print("This is the function you want to wrap.")
  # Define a decorator function
  
def my_decorator(original_function):
  print("This is the decorator which wraps other functionalities with the existing code")
  # define a wrapper function
  def wrapper_function():
    #add here the other functionalities you want to perform
    print("Hi! there")
    original_function()
  wrapper_function()

my_decorator(original_function)
This is the decorator which wraps other functionalities with the existing code
Hi! there
This is the function you want to wrap.

Wrapper Functions with Arguments

We can also pass arguments to the wrapper functions.

# defining decorator function
def func1(func2):
  def func2(a,b,*args,**kwargs):
    return func2(num1,num2,*args,**kwargs)
  return func2

def func2(a,b,*args,**kwargs):
  print("These are the arguments passed inside wrapped function")
  print('Number 1 =',num1,'Number 2 = ',num2)
  print('args:',args)
  print('kwargs:',kwargs)
 
num1 = int(input("Enter Number 1: "))
num2 = int(input("Enter Number 2: "))
func2(num1,num2,7,8,9,num3=22)
Enter Number 1: 2
Enter Number 2: 7
These are the arguments passed inside wrapped function
Number 1 = 2 Number 2 =  7
args: (7, 8, 9)
kwargs: {'num3': 22}

The asterisk unwraps the listing arguments into individual arguments which are then handed to main function to perform the desired operation.

Wrapper functions can also take a function along with the list of arguments as an argument. The code above demonstrates how you can pass function along with an argument to the wrapper function. Based on a function operation, the code returns the result.

def add(x, y):
    return x + y

def subtract(x, y):
    return x - y

def multiply(x, y):
    return x * y

def divide(x, y):
    return x / y

def result(func, a, b):
    ans = func(a, b)
    return ans

first_integer = int(input("Enter number 1: "))
second_integer = int(input("Enter number 2: "))

print("Sum: ", result(add, first_integer, second_integer))
print("Difference: ", result(subtract, first_integer, second_integer))
print("Product: ", result(multiply, first_integer, second_integer))
print("Division: ",  result(divide, first_integer, second_integer))
Enter number 1: 16
Enter number 2: 8
Sum:  24
Difference:  8
Product:  128
Division:  2.0

Now, lets see another example. Suppose you have a function which takes the list as an argument and returns the length of the list. Here, “wrapper_function” takes the function and list as an argument. The wrapper function calls the function argument and pass the list of arguments into this function which is “find_length” in this case. The find_length function returns the length of the arguments passed .

def wrapper_function(func, args):
    return func(*args)

def find_length(x):
    return(len(x))

output = wrapper_function(find_length, [[2, 9, 0, -5, 2]])
print(output)
5

In this tutorial, we have discussed wrapper functions in detail. You can pass a list of arguments and also functions to these wrapper functions. If you have any queries about this topic, let us know in the comments. Your feedback matters a lot for us. Contact us.

Leave a Comment

Your email address will not be published. Required fields are marked *