Book Image

Functional Python Programming

By : Steven F. Lott, Steven F. Lott
Book Image

Functional Python Programming

By: Steven F. Lott, Steven F. Lott

Overview of this book

Table of Contents (23 chapters)
Functional Python Programming
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Composite design


The common mathematical notation for a composite function looks as follows:

The idea is that we can define a new function, , that combines two other functions, and .

Python's multiple-line definition of the form is as follows:

@f
def g(x):
    something

This is vaguely equivalent to . The equivalence isn't very precise because the @f decorator isn't the same as the mathematical abstraction of composing and . For the purposes of discussing function composition, we'll ignore the implementation disconnect between the abstraction of and the @f decorator.

Because decorators wrap another function, Python offers a slightly more generalized composition. We can think of Python design as follows:

A decorator applied to some application function, , will include a wrapper function. One portion of the wrapper, , applies before the wrapped function and the other portion, , applies after the wrapped function.

The Wrapper() function often looks as follows:

@wraps(argument_function)
def something_wrapper...