1. Home
  2. Computing & Technology
  3. Python

Python Decorators, Methods, and Functions

From , former About.com Guide

1 of 7

Python's Function Decorators: An Introduction

Decorators were introduced to the Python world as part of the 2.4 release. They have since become increasingly importantin, especially for maintaining and extending Python programs. In order to best grasp the purpose and beneficiality of Python's decorators, it is important to understand what happens when one imports modules.

When a module is imported, the content of that module is inserted into your code at the point it is imported. So, when you import the re module or the sys module, Python stops reading your code when it hits "import <module name>", reads the given module from the code library, and then returns to your code. The module becomes part of the Python bytecode which is then converted to machine readable code for execution.

In this way, the Python library allows the programmer to nuance the pre-written code to a desired specification. Similarly, a decorator allows one to customise the use of a function, adding to it as desired without changing the original function. This discussion will rely on your having a good working knowledge of Python functions and classes. If you need to refresh yourself on functions, see my discussions of the syntax and purpose of Python functions and classes.

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. Programming Python Functions - Decorating Python Functions

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

All rights reserved.