10.9. Functional First-Class
Function can be assigned to variable
Function can be stored in data structures such as hash tables, lists, ...
Function can be returned
Function can be user as a parameter
In Python, functions are considered first-class citizens, which means that they can be treated like any other object in the language. This concept is also known as first-class functions.
Here are some of the properties of first-class functions in Python:
1. A function can be assigned to a variable: You can assign a function to a variable, just like you would with any other object.
2. A function can be passed as an argument to another function: You can pass a function as an argument to another function.
3. A function can be returned as a value from another function: A function can return another function as its value.
4. A function can be stored in a data structure: You can store functions in lists, dictionaries, or other data structures.
Here's an example of using a function as a first-class citizen in Python:
def add(a, b):
return a + b
def multiply(a, b):
return a * b
def apply(func, a, b):
return func(a, b)
apply(add, 2, 3)
5
apply(multiply, 2, 3)
6
In this example, the apply()
function takes a function as its first
argument, and two numbers as the second and third arguments. It then calls
the function with the two numbers and returns the result. The add()
and
multiply()
functions are passed as arguments to the apply()
function,
and the results are stored in result1
and result2
.
10.9.1. Function as Variable
Function can be assigned to variable
A function can be assigned to a variable: You can assign a function to a variable, just like you would with any other object.
SetUp:
def increment(x):
return x + 1
Usage:
func = increment
func(1)
2
10.9.2. Function in Data Structures
Function can be stored in data structures such as tuples, lists, dicts...
A function can be stored in a data structure: You can store functions in lists, dictionaries, or other data structures.
SetUp:
def increment(x):
return x + 1
def square(x):
return x ** 2
def decrement(x):
return x - 1
Usage:
funcs = (increment, square, decrement)
10.9.3. Functions as Return Values
Function can be returned
A function can be returned as a value from another function: A function can return another function as its value.
Definition:
def increment(x):
return x + 1
def get_operation():
return increment
Usage:
func = get_operation()
func(1)
2
10.9.4. Functions as Parameters
Function can be user as a parameter
A function can be passed as an argument to another function: You can pass a function as an argument to another function.
Definition:
def increment(x):
return x + 1
def apply(func, x):
return func(x)
Usage:
apply(increment, 1)
2
10.9.5. Use Case - 1
def map(func, data):
...
def filter(func, data):
...
def reduce(func, data):
...
10.9.6. Use Case - 2
Definition:
def square(x):
return x ** 2
def apply(fn, data):
return fn(data)
Usage:
apply(square, 2)
4
10.9.7. Use Case - 3
from urllib.request import urlopen
def fetch(url: str,
on_success = lambda response: ...,
on_error = lambda error: ...,
) -> None:
try:
result = urlopen(url).read().decode('utf-8')
except Exception as error:
on_error(error)
else:
on_success(result)
fetch(
url = 'https://python3.info',
on_success = lambda resp: print(resp),
on_error = lambda err: print(err),
)
def ok(response: str):
print(response)
def err(error: Exception):
print(error)
fetch(url='https://python3.info')
fetch(url='https://python3.info', on_success=ok)
fetch(url='https://python3.info', on_error=err)
fetch(url='https://python3.info', on_success=ok, on_error=err)
fetch(url='https://python3.info/not-existing', on_error=err)