blueloveTH 2 éve
szülő
commit
f6865aa595
2 módosított fájl, 8 hozzáadás és 15 törlés
  1. 5 5
      python/functools.py
  2. 3 10
      tests/44_decorator.py

+ 5 - 5
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

+ 3 - 10
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: