Higher Order Functions#

In Python functions are treated as first class citizens, allowing you to perform the following operations on functions:

  • A function can take one or more functions as parameters

  • A function can be returned as a result of another function

  • A function can be modified

  • A function can be assigned to a variable

In this section, we will cover:

  1. Handling functions as parameters

  2. Returning functions as return value from another functions

  3. Using Python closures and decorators

Function as a Parameter#

def sum_numbers(nums):
    return sum(nums)


def higher_order_function(f, lst):
    summation = f(lst)
    return summation


result = higher_order_function(sum_numbers, [1, 2, 3, 4, 5])
result
15

Function as a Return Value#

def square(x):
    return x**2


def cube(x):
    return x**3


def absolute(x):
    if x >= 0:
        return x
    else:
        return -(x)


def higher_order_function(type):
    if type == "square":
        return square
    elif type == "cube":
        return cube
    elif type == "absolute":
        return absolute


result = higher_order_function("square")
print(result(3))

result = higher_order_function("cube")
print(result(3))

result = higher_order_function("absolute")
print(result(-3))
9
27
3

You can see from the above example that the higher order function is returning different functions depending on the passed parameter.

Python Closures#

Python allows a nested function to access the outer scope of the enclosing function. This is is known as a Closure. Let us have a look at how closures work in Python. In Python, closure is created by nesting a function inside another encapsulating function and then returning the inner function.

def add_ten():
    ten = 10

    def add(num):
        return num + ten

    return add


closure_result = add_ten()
print(closure_result(5))
print(closure_result(10))
15
20

Python Decorators#

A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure. Decorators are usually called before the definition of a function you want to decorate.

Creating Decorators#

To create a decorator function, we need an outer function with an inner wrapper function.

"""This decorator function is a higher order function
that takes a function as a parameter"""


def uppercase_decorator(function):
    def wrapper():
        func = function()
        make_uppercase = func.upper()
        return make_uppercase

    return wrapper


@uppercase_decorator
def greeting():
    return "Welcome to Python"


print(greeting())
WELCOME TO PYTHON

Applying Multiple Decorators to a Single Function#

"""These decorator functions are higher order functions
that take functions as parameters"""


# First Decorator
def uppercase_decorator(function):
    def wrapper():
        func = function()
        make_uppercase = func.upper()
        return make_uppercase

    return wrapper


# Second decorator
def split_string_decorator(function):
    def wrapper():
        func = function()
        splitted_string = func.split()
        return splitted_string

    return wrapper


# order with decorators is important in this case - .upper() function does not work with lists
@split_string_decorator
@uppercase_decorator
def greeting():
    return "Welcome to Python"


print(greeting())
['WELCOME', 'TO', 'PYTHON']

Accepting Parameters in Decorator Functions#

Most of the time we need our functions to take parameters, so we might need to define a decorator that accepts parameters.

def decorator_with_parameters(function):
    def wrapper_accepting_parameters(para1, para2, para3):
        function(para1, para2, para3)
        print("I live in {}".format(para3))

    return wrapper_accepting_parameters


@decorator_with_parameters
def print_full_name(first_name, last_name, country):
    print("I am {} {}. I love to Python.".format(first_name, last_name, country))


print_full_name("Aurora", "Luna", "Indonesia")
I am Aurora Luna. I love to Python.
I live in Indonesia