
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How do you make a higher order function in Python?
A function in Python with another function as an argument or returns a function as an output is called the High order function. Let’s see the propertie −
The function can be stored in a variable.
The function can be passed as a parameter to another function.
The high order functions can be stored in the form of lists, hash tables, etc.
Function can be returned from a function.
Let’s see some examples −
Functions as objects
Example
The functions are considered as object in this example. Here, the function demo() is assigned to a variable −
# Creating a function def demo(mystr): return mystr.swapcase() # swapping the case print(demo('Thisisit!')) sample = demo print(sample('Hello'))
Output
tHISISIT! hELLO
Pass the function as an argument
Example
In this the function is passed as an argument. The demo3() function calls demo() and demo2() function as a parameter.
def demo(text): return text.swapcase() def demo2(text): return text.capitalize() def demo3(func): res = func("This is it!") # Function passed as an argument print (res) # Calling demo3(demo) demo3(demo2)
Output
tHIS IS IT! This is it!
Now, let us work around Decorators. We can use decorators as higher order functions.
Decorators in Python
Example
In Decorators, the functions are taken as an argument into another function and then called inside the wrapper function. Let us see a quick example −
@mydecorator def hello_decorator(): print("This is sample text.")
The above can also be written as −
def demo_decorator(): print("This is sample text.") hello_decorator = mydecorator (demo_decorator)
Decorator Example
Example
In this example, we will work around Decorator as higher order function −
def demoFunc(x,y): print("Sum = ",x+y) # outer function def outerFunc(sample): def innerFunc(x,y): # inner function return sample(x,y) return innerFunc # calling demoFunc2 = outerFunc(demoFunc) demoFunc2(10, 20)
Output
Sum = 30
Example
def demoFunc(x,y): print("Sum = ",x+y) # outer function def outerFunc(sample): def innerFunc(x,y): # inner function return sample(x,y) return innerFunc # calling demoFunc2 = outerFunc(demoFunc) demoFunc2(10, 20)
Output
Sum = 30
Applying Syntactic Decorator
Example
The above example can be simplified using decorator with the @symbol. This eases applying decorators by placing @ symbol before the function we want to decorate −
# outer function def outerFunc(sample): def innerFunc(x,y): # inner function return sample(x,y) return innerFunc @outerFunc def demoFunc(x,y): print("Sum = ",x+y) demoFunc(10,20)
Output
Sum = 30
- Related Articles
- Higher-Order Function in Golang
- How do you make an array in Python?
- How do you use Python to make websites?
- How do you test that a Python function throws an exception?
- How do you make a list iterator in Java?
- How do you make code reusable in C#?
- Higher order components in React.js
- How do you make a button that adds text in HTML ?
- How do you make a shallow copy of a list in Java?
- How do you conduct function Testing?
- How do you make synchronous HTTP request in JavaScript?
- How do you make a string in PHP with a backslash in it?
- Functools — Higher-order functions and operations on callable objects in Python
- How do you sort an array in C# in ascending order?
- How do you sort an array in C# in descending order?
