作为装饰器实现的简单属性检查器
attrcheck的Python项目详细描述
属性检查:属性检查
This module provides a simple attribution checker implemented as a decorator. All functionality are provided as keyword arguments of the decorator.
Sample example of usage is following:
>>> from attrcheck import attrcheck >>> @attrcheck(x=['real'], y=['index', 'strip'], z=dir(list)) ... def foo(x, y, z=[]): pass上面的代码表示以下内容:
>>> def foo(x, y, z=[]): ... if not hasattr(x, 'real'): ... raise AttributeError ... if not hasattr(y, 'index'): ... raise AttributeError ... if not hasattr(y, 'strip'): ... raise AttributeError ... for name in dir(list): ... if not hasattr(z, name): ... raise AttributeError此外,attrcheck可以检查默认参数值。 因此,下面的代码抛出attributerror。
>>> @attrcheck(y=dir(str)) ... def bar(x, y=[]): pass