Skip to Content
Mastering Object-oriented Python
book

Mastering Object-oriented Python

by Steven F. Lott
April 2014
Beginner to intermediate
634 pages
15h 22m
English
Packt Publishing
Content preview from Mastering Object-oriented Python

Parameterizing a decorator

Sometimes we want to provide more elaborate parameters to a decorator. The idea is that we are going to customize the wrapping function. When we do this, decoration becomes a two-step process.

When we write the following code, we provide a parameterized decorator to a function definition:

@decorator(arg)
def func( ):
    pass

The use of the decorator is a shorthand for the following code:

def func( ):
    pass
func= decorator(arg)(func)

Both examples do the following three things:

  • Defined a function, func
  • Applied the abstract decorator to its arguments to create a concrete decorator, decorator(arg)
  • Applied the concrete decorator to the defined function to create the decorated version of the function, decorator(arg)(func)

This means that ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Object-Oriented Python - Second Edition

Mastering Object-Oriented Python - Second Edition

Steven F. Lott

Publisher Resources

ISBN: 9781783280971Supplemental Content