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

Adding a parameter to a decorator


A common requirement is to customize a decorator with additional parameters. Rather than simply creating a composite , we're doing something a bit more complex. We're creating . We've applied a parameter, c, as part of creating the wrapper. This parameterized composite,, can then be used with the actual data, x.

In Python syntax, we can write it as follows:

@deco(arg)
def func( ):
    something

This will provide a parameterized deco(arg) function to the base function definition.

The effect is as follows:

def func( ):
    something
func= deco(arg)(func)

We've done three things and they are as follows:

  1. Define a function, func.

  2. Apply the abstract decorator, deco(), to its arguments to create a concrete decorator, deco(arg).

  3. Apply the concrete decorator, deco(arg), to the base function to create the decorated version of the function, deco(arg)(func).

A decorator with arguments involves indirect construction of the final function. We seem to have moved beyond merely...