@decorator syntax is sugar, but for what exactly? (decorator libraries).
Skip Montanaro
skip at pobox.com
Tue Aug 10 13:14:57 EDT 2004
Roy> Does the proposed mechanism support something like (to use one of
Roy> Dan's exmaples, written with two different syntaxen):
...
Yes (using a class instead of a module simply for convenience):
class martha:
memo = {}
@staticmethod
def memoize(func):
if func not in martha.memo:
martha.memo[func] = {}
def _inner(*args, **kwds):
items = kwds.items()
items.sort()
items = tuple(items)
key = (args, items)
try:
val = martha.memo[key]
except KeyError:
val = func(*args, **kwds)
martha.memo[key] = val
return val
return _inner
@martha.memoize
def fib(n):
assert n >= 0
print n
if n <= 1:
return 1
return n + fib(n-1)
print fib(5)
print fib(4)
Running that yields this output:
5
4
3
2
1
15
10
Skip
More information about the Python-list
mailing list