diff --git a/python/functools.py b/python/functools.py index 81e517fa..a847efc1 100644 --- a/python/functools.py +++ b/python/functools.py @@ -1,9 +1,9 @@ def cache(f): def wrapper(*args): - if not hasattr(f, 'cache'): - f.cache = {} + if not hasattr(f, '__cache__'): + f.__cache__ = {} key = args - if key not in f.cache: - f.cache[key] = f(*args) - return f.cache[key] + if key not in f.__cache__: + f.__cache__[key] = f(*args) + return f.__cache__[key] return wrapper \ No newline at end of file diff --git a/tests/44_decorator.py b/tests/44_decorator.py index 6de0d06b..ae3a81f3 100644 --- a/tests/44_decorator.py +++ b/tests/44_decorator.py @@ -1,14 +1,7 @@ +from functools import cache -def cache(f): - def wrapper(*args): - if not hasattr(f, 'cache'): - f.cache = {} - key = args - if key not in f.cache: - f.cache[key] = f(*args) - return f.cache[key] - return wrapper - +@cache +@cache @cache def fib(n): if n < 2: