Python

  1. Home
  2. Computing & Technology
  3. Python

Python Decorators, Methods, and Functions

From Al Lukaszewski, for About.com

6 of 7

Decorating Functions in Python

Next, we need to decorate some functions. Let's create three functions. The first two will be decorated by decorate and ornament, respectively. The third will be created by both.

>>> @decorate
... def a(): pass
...
>>> @ornament
... def b(): pass
...
>>> @decorate
... @ornament
... def c(): pass

Now simply type the names -- not even the calls of the functions in turn. You will see that they are all residing as objects in memory.

>>> a
<function a at 0xb7de7df4>
>>> b
<function b at 0xb7de7e2c>
>>> c
<function c at 0xb7de7e9c>
But what about the attributes? That is discussed on the next page.

Explore Python

About.com Special Features

Python

  1. Home
  2. Computing & Technology
  3. Python
  4. Beginning Python
  5. Python Function Decorators: Decorating Functions in Python

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

All rights reserved.