首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

python 装饰器有关的有关问题

2012-04-11 
python 装饰器有关的问题1.装饰器问题。我注释掉的那一行代码,添加上也可以正常执行。原程序是有这一行的,我

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@memodef 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.
...

热点排行