我试图对类的成员函数返回的值使用@postcondition修饰符,如下所示:
def out_gt0(retval, inval):
assert retval > 0, "Return value < 0"
class foo(object):
def __init__(self, w, h):
self.width = w
self.height = h
@postcondition(out_gt0)
def bar(self):
return -1
当我试图调用成员函数“bar”(从而激发@postcondition提供警告)时,我得到了这样的结果:
>>> f = foo(2,3)
>>> f.bar()
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
f.bar()
File "<pyshell#8>", line 106, in __call__
result = self._func(*args, **kwargs)
TypeError: bar() takes exactly 1 argument (0 given)
>>>
我对@postcondition的定义就是这里看到的http://wiki.python.org/moin/PythonDecoratorLibrary#Pre-.2FPost-Conditions。
我假设出现这个错误是因为@postcondition下面的函数不希望处理成员函数(当然,我看到的所有示例都只是使用普通的旧函数),但我不确定如何修复它以便可以这样做?
如有任何建议,将不胜感激。
目前没有回答
相关问题 更多 >
编程相关推荐