【发布时间】:2012-06-25 18:54:18
【问题描述】:
在我的一些代码中,我使用了 Python 装饰器库中漂亮的 memoized 类。
我正在使用的库之一对函数使用自省来获取它需要的参数数量,但在装饰函数上失败。具体来说,它检查co_argcount 变量。
if (PyInt_AsLong(co_argcount) < 1) {
PyErr_SetString(PyExc_TypeError, "This function has no parameters to mini\
mize.");
似乎 argcount 没有被转移到记忆函数中。
>>> def f(x):
... return x
...
>>> f.func_code.co_argcount
1
>>> g = memoized(f)
>>> g.func_code.co_argcount
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'memoized' object has no attribute 'func_code'
如何修改 memoized 类,使我的 memoized 函数看起来、尝起来和闻起来都像原始函数?
【问题讨论】:
标签: python memoization