专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Perl/Python

python 装饰器有关的有关问题

发布时间:2011-06-29 20:10:58 文章来源:www.iduyao.cn 采编人员:星星草
python 装饰器有关的问题
1.装饰器问题。我注释掉的那一行代码,添加上也可以正常执行。原程序是有这一行的,我没懂所以注释掉了。句法:装饰器包装函数.装饰器函数 = {} (fmemo.memo = table)
Python code

def memo(f):
    "Memoize function f."
    table = {}
    def fmemo(*args):
        if args not in table:
            table[args] = f(*args)
        return table[args]
##    fmemo.memo = table
    return fmemo

@memo
def segment(text):
    "Return a list of words that is the best segmentation of text."
    if not text: return []
    candidates = ([first]+segment(rem) for first,rem in splits(text))
    return max(candidates, key=Pwords)




其实不用管代码逻辑,只要看看语法就好。问题感觉从来没接触过,谢谢。

------解决方案--------------------
貌似就只是加个属性记住table,那么后面代码里,你可以用segment.momo来读取这个table
------解决方案--------------------
看看文档有关language reference 3.1节提到:
...
Function objects also support getting and setting arbitrary attributes, which can be used, for example, to attach metadata to functions. Regular attribute dot-notation is used to get and set such attributes. Note that the current implementation only supports function attributes on user-defined functions. Function attributes on built-in functions may be supported in the future.
...
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: