Passing function as an argument in Python (original) (raw)
Last Updated : 01 Mar, 2025
In Python, functions are first-class objects meaning they can be assigned to variables, passed as arguments and returned from other functions. This enables higher-order functions, **decorators and lambda expressions. By passing a function as an argument, we can modify a function’s behavior dynamically without altering its implementation. **For Example:
Python `
def process(func, text): # applies a function to text
return func(text)
def uppercase(text): # converts text to uppercase
return text.upper()
print(process(uppercase, "hello"))
`
**Explanation: **process() applies a given function to **text and **uppercase() converts text to uppercase. Passing uppercase to process with “hello” results in “HELLO”.
Table of Content
- Higher Order Functions
- Lambda Functions
- Wrapper Functions(Decorators)
- Built-in Functions using function arguments
- Passing function as an argument in Python – FAQs
Higher Order Functions
A higher-order function takes or returns another function, enabling reusable and efficient code. It supports functional programming with features like callbacks, decorators, and utilities such as map(), filter(), and reduce().
**Example 1 : Basic function passing
Python `
higher-order function
def fun(func, number): return func(number)
function to double a number
def double(x): return x * 2
print(fun(double, 5))
`
**Explanation: fun() takes **double() as an argument and applies it to 5, returning 10.
**Example 2: Passing Built-in Functions
Python `
function to apply an operation on a list
def fun(func, numbers): return [func(num) for num in numbers]
using the built-in 'abs' function
a = [-1, -2, 3, -4] print(fun(abs, a))
`
**Explanation: abs() is passed to **fun(), which applies it to each element in the list, converting negative numbers to positive.
Lambda Functions
A lambda function in Python is a small, anonymous function with a single expression, defined using lambda. It’s often used in higher-order functions for quick, one-time operations.
**Example: Lambda Function as an Argument
Python `
function that applies an operation to a number
def fun(func, number): return func(number)
passing a lambda function
print(fun(lambda x: x ** 2, 5))
`
**Explanation: lambda x: x ** 2 is passed to fun(), which squares the input 5 to produce 25.
Wrapper Functions(Decorators)
A wrapper function (decorator) enhances another function’s behavior without modifying it. It takes a function as an argument and calls it within the wrapper.
**Example 1 : Simple decorator
Python `
simple decorator example
def decorator_fun(original_fun): def wrapper_fun(): print("Hello, this is before function execution") original_fun() print("This is after function execution") return wrapper_fun
@decorator_fun def display(): print("This is inside the function !!")
calling the decorated function
display()
`
Output
Hello, this is before function execution This is inside the function !! This is after function execution
**Explanation: decorator_fun wraps the display() function, adding pre- and post-execution messages.
**Example 2: Lambda Wrapper Function
Python `
def apply_lambda(func, value): return func(value)
square = lambda x: x ** 2 print("Square of 2 is:", apply_lambda(square, 2))
`
**Explanation: apply_lambda() function applies the lambda function **lambda x: x ** 2 to 2, returning 4.
Built-in Functions using function arguments
Python provides built-in functions that take other functions as arguments .
**Example 1 : map()
Python `
a = [1, 2, 3, 4] res = list(map(lambda x: x * 2, a)) print(res)
`
**Explanation: map() function applies lambda x: x * 2 to each element in **a, doubling all values.
**Example 2 : filter()
Python `
a = [1, 2, 3, 4, 5] res = list(filter(lambda x: x % 2 == 0, a)) print(res)
`
**Explanation: filter() selects even numbers from the list using lambda x: x % 2 == 0.
**Example 3: reduce()
Python `
from functools import reduce a = [1, 2, 3, 4]
res = reduce(lambda x, y: x + y, a) print(res)
`
**Explanation: reduce() function applies **lambda x, y: x + y cumulatively to elements in a, summing them to 10.