Function as an Argument

Functions in Python are first class-citizens. This means that they support operations such as being passed as an argument, returned from a function, modified, and assigned to a variable.

wrapper function is a function that allows wrapping another function to extend the behavior of the wrapped function without permanently modifying it.

Let's take a closer look at the lambda wrapper function. The lambda function is an anonymous function that accepts any number of arguments but can only have one expression.

To create this function, use the lambda keyword:

sum = lambda a, b: a + b
double = lambda x: x * 2

You can use a lambda function as a parameter in another function:

add = lambda x: x + 2
subtract = lambda x: x - 2 
double = lambda x: x * 2
divided = lambda x: x / 2

def calculate(operation, number):
    return operation(number)

calculate(add, 10)      # 12
calculate(subtract, 10) # 8
calculate(double, 10)       # 20
calculate(divided, 10)      # 5
from typing import Callable


def new_number(number: int) -> None:

print(f"Received a new number: {number}")




def is_positive(number: int) -> None:

if number > 0:

print(f"{number} is a positive number")

if number == 0:

print("Zero")

if number < 0:

print(f"{number} is a negative number")




def print_bye(number: int) -> None:

print("Bye!")




def numbers_handler(

numbers: list,

before : Callable[[int], None] = new_number,

action : Callable[[int], None] = is_positive,

after : Callable[[int], None] = print_bye,

) -> None:

for number in numbers:

before(number)

action(number)

after(number)