Home > Backend Development > Python Tutorial > How to create higher-order functions in Python?

How to create higher-order functions in Python?

PHPz
Release: 2023-09-05 19:29:05
forward
1345 people have browsed it

How to create higher-order functions in Python?

In Python, a function that takes another function as a parameter or returns a function as output is called a higher-order function. Let's take a look at its features -

  • This function can be stored in a variable.

  • This function can be passed as a parameter to another function.

  • Higher-order functions can be stored in lists, hash tables, etc.

  • Functions can be returned from functions.

Let’s look at some examples −

Function as Object

The Chinese translation of

Example

is:

Example

In this example, these functions are treated as objects. 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'))
Copy after login

Output

tHISISIT!
hELLO
Copy after login

Passing functions as parameters

The Chinese translation of

Example

is:

Example

Passed as a parameter in this function. demo3() The function calls the demo() and demo2() functions as parameters.

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)
Copy after login

Output

tHIS IS IT!
This is it!
Copy after login

Now, let’s discuss decorators. We can use decorators as higher order functions.

Decorators in Python

The Chinese translation of

Example

is:

Example

In a decorator, a function is passed as a parameter to another function and then called in the wrapping function. Let’s look at a quick example −

@mydecorator
def hello_decorator():
   print("This is sample text.")
Copy after login

The above can also be written as -

def demo_decorator():
   print("This is sample text.")
hello_decorator = mydecorator (demo_decorator)
Copy after login

Decorator example

The Chinese translation of

Example

is:

Example

In this example, we will work with decorators as higher-order functions -

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)
Copy after login
Copy after login

Output

Sum = 30
Copy after login
Copy after login
Copy after login
The Chinese translation of

Example

is:

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)
Copy after login
Copy after login

Output

Sum = 30
Copy after login
Copy after login
Copy after login

Apply syntax decorator

The Chinese translation of

Example

is:

Example

The above example can be simplified using a decorator with @symbol. The application of decorators can be simplified by placing the @ 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)
Copy after login

Output

Sum = 30
Copy after login
Copy after login
Copy after login

The above is the detailed content of How to create higher-order functions in Python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template