xxxxxxxxxx
import functools
def power(a, b):
return a ** b
square = functools.partial(power, b = 2)
cube = functools.partial(power, b = 3)
print(square(5))
print(cube(5))
xxxxxxxxxx
partialfunction
allow one to derive a function with x parameters to a function with fewer
parameters and fixed values set for the more limited function.
from functools import partial
# A normal function
def f(a, b, c, x):
return 1000*a + 100*b + 10*c + x
# A partial function that calls f with
# a as 3, b as 1 and c as 4.
g = partial(f, 3, 1, 4)
# Calling g()
print(g(5))
xxxxxxxxxx
"""
Partial functions allow us to fix a certain number of arguments
of a function and generate a new function.
Partial functions can be used to derive specialized functions from
general functions and therefore help us to reuse our code.
"""
from functools import partial
# A normal function
def f(a, b, c, x):
return 1000*a + 100*b + 10*c + x
# A partial function that calls f with
# a as 3, b as 1 and c as 4.
g = partial(f, 3, 1, 4)
# Calling g()
print(g(5))