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