Function Composition
Function composition is the process of combining simple functions to build more complex ones e.g. quoting strings.
Example
""" function_composition.py """
def double_quote(func):
""" double quote """
def wrapper(*args, **kwargs):
return ''.join(['"', func(*args, **kwargs), '"'])
return wrapper
def single_quote(func):
""" single quote """
def wrapper(*args, **kwargs):
return ''.join(["'", func(*args, **kwargs), "'"])
return wrapper
def repeat(substring, count):
""" repeat substring count times """
return substring * count
repeat_double = double_quote(repeat)
repeat_single = single_quote(repeat)
print(repeat_double('abc', 3))
print(repeat_single('abc', count=3))
