Use class and function decorators -- even in Python 2.3!
Project description
(NEW in 1.1: The struct() decorator makes it easy to create tuple-like data structure types, by decorating a constructor function.)
Want to use decorators, but still need to support Python 2.3? Wish you could have class decorators, or decorate arbitrary assignments? Then you need “DecoratorTools”. Some quick examples:
# Method decorator example from peak.util.decorators import decorate class Demo1(object): decorate(classmethod) # equivalent to @classmethod def example(cls): print "hello from", cls # Class decorator example from peak.util.decorators import decorate_class def my_class_decorator(): def decorator(cls): print "decorating", cls return cls decorate_class(decorator) class Demo2: my_class_decorator() # "decorating <class Demo2>" will be printed when execution gets here
Installing DecoratorTools (using "easy_install DecoratorTools" or "setup.py install") gives you access to the peak.util.decorators module. The tools in this module have been bundled for years inside of PEAK, PyProtocols, RuleDispatch, and the zope.interface package, so they have been widely used and tested. (Unit tests are also included, of course.)
This standalone version is backward-compatible with the bundled versions, so you can mix and match decorators from this package with those provided by zope.interface, TurboGears, etc.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.