Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to get list of parameters name from a function in Python?
Next article icon

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:

Python
defprocess(func,text):# applies a function to textreturnfunc(text)defuppercase(text):# converts text to uppercasereturntext.upper()print(process(uppercase,"hello"))

Output
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))

Output
10

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

Output
[1, 2, 3, 4]

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

Output
25

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

Output
Hello, 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

Python
defapply_lambda(func,value):returnfunc(value)square=lambdax:x**2print("Square of 2 is:",apply_lambda(square,2))

Output
Square of 2 is: 4

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

Python
a=[1,2,3,4]res=list(map(lambdax:x*2,a))print(res)

Output
[2, 4, 6, 8]

Explanation: map() function applies lambda x: x * 2to each element ina, doubling all values.

Example 2 :filter()

Python
a=[1,2,3,4,5]res=list(filter(lambdax:x%2==0,a))print(res)

Output
[2, 4]

Explanation: filter()selects even numbers from the list using lambda x: x % 2 == 0.

Example 3:reduce()

Python
fromfunctoolsimportreducea=[1,2,3,4]res=reduce(lambdax,y:x+y,a)print(res)

Output
10

Explanation: reduce() function applieslambda x, y: x + y cumulatively to elements in a, summing them to 10.


Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp