1. Home
  2. Computing & Technology
  3. Python

Python Decorators, Methods, and Functions

From , former About.com Guide

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.

Explore Python
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Python
  4. Beginning Python
  5. Python Function Decorators: Redirecting Calls to Functions and Methods

©2009 About.com, a part of The New York Times Company.

All rights reserved.