Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Defining Complex Decorators

A decorator transforms an argument function definition into a result function definition. In addition to a function, we can also provide argument values to a decorator. These more complex decorators involve a two-step dance that creates an intermediate function as well as the final result function.

The first step evaluates the abstract decorator to create a concrete decorator. The second step applies the concrete decorator to the argument function. This second step is what a simple decorator does.

Assume we have some qualified decorator, for example @debug( flag ), where flag can be True to enable debugging and False to disable debugging. Assume we provide the following function definition.

debugOption= True
class MyClass( object ):
    @debug( debugOption )
    def someMethod( self, args ):
        
real work

Here's what happens when Python creates the definition of the someMethod function.

  1. Define the argument function, someMethod.

  2. Evaluate the abstract decorator debug( debugOption ) to create a concrete decorator based on the argument value.

  3. Apply the concrete decorator the the argument function, someMethod.

  4. The result of the concrete decorator is the result function, which is given the name someMethod.

Here's an example of one of these more complex decorators. Note that these complex decorators work by creating and return a concrete decorators. Python then applies the concrete decorators to the argument function; this does the work of transforming the argument function to the result function.

Example 26.4. debug.py

def debug( theSetting ):
    def concreteDescriptor( aFunc ):
        if theSetting:
            def debugFunc( *args, **kw ):
                print "enter", aFunc.__name__
                return aFunc( *args, **kw )
            debugFunc.__name__= aFunc.__name__
            debugFunc.__doc__= aFunc.__doc__
            return debugFunc
        else:
            return aFunc
    return concreteDescriptor
1

This is the concrete decorators, which is created from the argument, theSetting.

2

If theSetting is True, the concrete decorator will create the result function named debugFunc, which prints a message and then uses the argument function.

3

If theSetting is False, the concrete descriptor will simply return the argument function without any overhead.


 
 
  Published under the terms of the Open Publication License Design by Interspire