类、函数和元类装饰器——甚至在Python2.3中(现在已经对生成的代码进行了源代码调试)!
DecoratorTools的Python项目详细描述
想要使用decorators,但仍然需要支持python 2.3?希望你能 让班级装饰人员,装饰任意作业,或搭配装饰 函数签名到其原始函数?想得到元类 不创建元类的功能?同步方法怎么样?
“decoratortools”能帮你解决所有这些问题。一些快速示例:
# 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
安装decoratortools(使用"easy_install DecoratorTools"或 "setup.py install")允许您访问peak.util.decorators 模块。本模块中的工具在Peak内部捆绑多年, pyprotocols、ruledispatch和zope.interface包,所以它们已经 广泛使用和测试。(当然,单元测试也包括在内。)
此独立版本与捆绑版本向后兼容,因此 可以将此包中的装饰器与 zope.interface、涡轮齿轮等
有关完整文档,请参见DecoratorTools manual。
版本1.7之后的更改:
- The ^{tt4}$ decorator now supports using a return value instead of a docstring, in order to work with the “-OO” option to Python; it’s highly recommended that you update your template functions to use a return value instead of a docstring. (The error message has also been improved for the missing docstring case.)
- Fixed metaclass collisions in ^{tt5}$ subclasses that mix in abstract classes (e.g. ^{tt6}$) in Python 2.6+.
版本1.6之后的更改:
- Added ^{tt7}$ decorator to support locking objects during method execution.
版本1.5之后的更改:
- Added ^{tt5}$ base class that allows you to do the most often-needed metaclass behviors without needing an actual metaclass.
版本1.4之后的更改:
- Added ^{tt9}$ function, so that complex decorators that call DecoratorTools functions while being called by DecoratorTools functions, will work correctly.
版本1.3之后的更改:
- Added support for debugging generated code, including the code generated by ^{tt10}$ and ^{tt11}$.
版本1.2之后的更改:
- Added ^{tt10}$ function and ^{tt11}$ decorator to support signature matching for decorated functions. (These features are similar to the ones provided by Michele Simionato’s “decorator” package, but do not require Python 2.4 and don’t change the standard idioms for creating decorator functions.)
- ^{tt14}$ will no longer apply duplicate class decorator callbacks unless the ^{tt15}$ argument is true.
版本1.1之后的更改:
- Fixed a problem where instances of different struct types could equal each other
版本1.0之后的更改:
- The ^{tt16}$ decorator makes it easy to create tuple-like data structure types, by decorating a constructor function.