InPython,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,decoratorsandlambda expressions. By passing a function as an argument, we can modify a function’s behavior dynamically without altering its implementation.For Example:
Pythondefprocess(func,text):# applies a function to textreturnfunc(text)defuppercase(text):# converts text to uppercasereturntext.upper()print(process(uppercase,"hello"))
Explanation:process()applies a given function totextanduppercase()converts text to uppercase. Passing uppercase to process with "hello" results in "HELLO".
Higher Order Functions
Ahigher-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 functiondeffun(func,number):returnfunc(number)# function to double a numberdefdouble(x):returnx*2print(fun(double,5))
Explanation: fun() takesdouble() as an argument and applies it to 5, returning 10.
Example 2:Passing Built-in Functions
Python# function to apply an operation on a listdeffun(func,numbers):return[func(num)fornuminnumbers]# using the built-in 'abs' functiona=[-1,-2,3,-4]print(fun(abs,a))
Explanation: abs() is passed tofun(), which applies it to each element in the list, converting negative numbers to positive.
Lambda Functions
A lambda functionin 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 numberdeffun(func,number):returnfunc(number)# passing a lambda functionprint(fun(lambdax:x**2,5))
Explanation: lambda x: x ** 2 is passed to fun(), which squares the input 5 to produce 25.
Wrapper Functions(Decorators)
Awrapper 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 exampledefdecorator_fun(original_fun):defwrapper_fun():print("Hello, this is before function execution")original_fun()print("This is after function execution")returnwrapper_fun@decorator_fundefdisplay():print("This is inside the function !!")# calling the decorated functiondisplay()
OutputHello, this is before function executionThis is inside the function !!This is after function execution
Explanation: decorator_funwraps the display()function, adding pre- and post-execution messages.
Example 2: Lambda Wrapper Function
Pythondefapply_lambda(func,value):returnfunc(value)square=lambdax:x**2print("Square of 2 is:",apply_lambda(square,2))
Explanation: apply_lambda()function applies the lambda functionlambda 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()
Pythona=[1,2,3,4]res=list(map(lambdax:x*2,a))print(res)
Explanation: map() function applies lambda x: x * 2to each element ina, doubling all values.
Example 2 :filter()
Pythona=[1,2,3,4,5]res=list(filter(lambdax:x%2==0,a))print(res)
Explanation: filter()selects even numbers from the list using lambda x: x % 2 == 0.
Example 3:reduce()
Pythonfromfunctoolsimportreducea=[1,2,3,4]res=reduce(lambdax,y:x+y,a)print(res)
Explanation: reduce() function applieslambda x, y: x + y cumulatively to elements in a, summing them to 10.