A function can take multiple arguments, these arguments can be objects, variables(of same or different data types) and functions. Python functions are first class objects. In the example below, a function is assigned to a variable. This assignment doesn’t call the function. It takes the function object referenced by shout and creates a second name pointing to it, yell.
Higher Order Functions
Because functions are objects we can pass them as arguments to other functions. Functions that can accept other functions as arguments are also called higher-order functions. In the example below, a function greet is created which takes a function as an argument.
HI, I AM CREATED BY A FUNCTION PASSED AS AN ARGUMENT. hi, i am created by a function passed as an argument.
Wrapper function or decorator allows us to wrap another function in order to extend the behavior of the wrapped function, without permanently modifying it. In Decorators, functions are taken as the argument into another function and then called inside the wrapper function. To know more about decorator click here.
Below is the example of a simple decorator.
Hello, this is before function execution This is inside the function !! This is after function execution
Lambda wrapper function
In Python, anonymous function means that a function is without a name. As we already know that
def keyword is used to define the normal functions and the
lambda keyword is used to create anonymous functions. This function can have any number of arguments but only one expression, which is evaluated and returned. Lambda function can also have another function as an argument. The below example shows a basic lambda function where another lambda function is passed as an argument.
square of 2 is :4 The cube of 4 is 64
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Pass list as command line argument in Python
- Command-Line Option and Argument Parsing using argparse in Python
- How to pass argument to an Exception in Python?
- Python Lambda with underscore as an argument
- Python: Passing Dictionary as Arguments to Function
- Python | Passing dictionary as keyword arguments
- Python | Passing Filenames to Extension in C
- Passing NULL-Terminated Strings to C Libraries
- Unicode Strings Passing to C Libraries
- Python - Call function from another function
- Wand function() function in Python
- Returning a function from a function - Python
- wxPython - GetField() function function in wx.StatusBar
- How to write an empty function in Python - pass statement?
- Function Decorators in Python | Set 1 (Introduction)
- Vulnerability in input() function – Python 2.x
- Function Annotations in Python
- Sorted() function in Python
- Ways to sort list of dictionaries by values in Python - Using lambda function
- Python Numbers | choice() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai