作为装饰器实现的简单属性检查器

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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java使用增强的forloop从两个ArrayList检索数据   如何在安卓 java中设置双精度3范围零   java PostgreSQL+Openshift:对于给定的数据库URL,可能是错误的驱动程序?   使用war时IBM Liberty(WLP)中的java组到角色映射   java Android league应用程序不断崩溃   Java错误仅允许对参数进行非法修改   为什么“enum”的实例字段在java中是“enum”?   java无法引用TextView   java SimpleJdbcInsert未在默认模式的表中插入数据   多线程Java多线程共享一个全局映射变量   java如何从WebView获取HTML内容进行打印?   JavaLibGDX:如何更改单个精灵的颜色?   测试Java程序退出函数不工作   欺骗HWID欺骗程序Java