1. Computing & Technology

Python Decorators, Methods, and Functions

From , former About.com Guide

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.

©2012 About.com. All rights reserved.

A part of The New York Times Company.