How to Pass a Method as an Argument in Python?
In this article, we will learn how to pass a method as an argument in Python. We will discuss the different methodology of passing methods and functions as arguments using custom codes as well.
In Python, everything such as objects, variables, etc. is treated as an object. Therefore, functions/methods, are too treated as objects. In this article, we will stick to methods. A method can take multiple arguments, like objects, variables(of same or different data types), and even other methods because python methods are first-class objects. Methods are callable objects so you can pass them, store them, and can do whatever you want to.
__call__ method is associated with every method and gets called automatically when you invoke the method with or without arguments. You can think about a method (or function) as a variable whose value is the actual callable code object. A user-defined method or a built-in method both can be passed as an argument to another method in python.
In this article, we will use methods in the following examples, but note that everything below applies identically to functions (except without the
self parameter). Functions and methods both are utility blocks of code, but when a utility function is defined inside a class, it is known as a method. Don’t get confused between methods and functions. All functions in Python can be passed as an argument to another function.
Example: A Class Method Passed as an Argument
Methods are passed as arguments just like a variable. In this example, we define a class and its objects. We create an object to call the class methods. Now, to call a passed method or function, you just use the name it's bound to in the same way you would use the method's (or function's) regular name.
Note: If you want to pass a method of a class as an argument but don't yet have the object on which you are going to call it, you can simply pass the object once you have it as the first argument (i.e. the "self" argument).
return "Hello World"
def method2(self, methodToRun):
result = methodToRun()
obj = my_class()
#method1 is passed as an argument
Example: Higher Order Functions Passed as an Argument
Just like class methods are called using class objects and are passed as arguments, a general user-defined function can also be passed as an argument to another function because functions are objects. Functions that can accept another function as arguments are called higher-order functions. In the example below, a function func1 is created which takes a function as an argument.
# storing the function in a variable
res = func("Hello World")
Example: Wrapper Function Passed as an Argument
In Python, Wrapper functions or decorators wrap another function in order to extend the behavior of the wrapped function, without permanently modifying it. In Decorators, functions are passed as the argument to another function and then they are called inside the wrapper function.
The below example defines a simple decorator hello_decorator. inner1 is a Wrapper function in which the argument is called. The inner function can access the outer local functions like in this case func(). func() is called inside the wrapper function.
print("Hello, this is before function execution")
print("This is after function execution")
# defining a function, to be called inside wrapper
print("This is inside the function !!")
# pass 'function_to_be_used' inside the decorator to control its behavior
function_to_be_used = hello_decorator(function_to_be_used)
# calling the function
Hello, this is before function execution
This is inside the function !!
This is after function execution
Example: Lambda Passed as an argument to map()
The most important example of passing methods as arguments is lambda. You often use
lambda together to perform various list operations and many more in python. In python, a lambda expression is a special syntax for creating an anonymous function. When you evaluate a lambda expression the object you get back is called a lambda function. Lambda functions are just like regular Python functions, with a few caveats.
Python implements the map() function where the first parameter is a function and the second is iterable. In this example, the function call passes the function sqr(x) as a function argument using the map method. Another way to use the map() method is to pass lambda as an argument with iterable.
return x ** 2
list1 = [1, 3, 5, 7, 9]
print(list(map((lambda x: x**2), list1)))
[1, 9, 25, 49, 81]
In this article, we learned to pass a method and function as arguments to another function. Every example has its own methodology. Lambda expression as an argument to map() method is the best example to understand this article because you have used them together multiple times. Try passing different functions as arguments to other functions and observe the behavior.