1. Technology

Python Decorators, Methods, and Functions


4 of 7

Redirecting Python Calls to Functions and Methods

Decorators allow the programmer to redirect a function call in a predictable and manageable fashion. The following is an example of the basic syntax.

 class Class: 
 Â Â Â Â def method(cls) 
 Â Â Â Â Â Â Â Â ... 
 Â Â Â Â @method 
 Â Â Â Â def function: 
 Â Â Â Â Â Â Â Â pass 
The goal of this new syntax is to make it easier for programmers to see the interrelationship between methods.

An incidental plus is that one can knit two different pieces of code together very cleanly without editing either the function or the function call. Simply create a function to match the call and have it decorated to the desired function.

This is the basic syntax, but a concrete example is really needed to illustrate this feature. The following pages look at the use of single and multiple decorators for functions and the value they hold for Python programming.

  1. About.com
  2. Technology
  3. Python
  4. Beginning Python
  5. Python Function Decorators: Redirecting Calls to Functions and Methods

©2014 About.com. All rights reserved.