blueloveTH 2 年 前
コミット
a23d100265
2 ファイル変更21 行追加11 行削除
  1. 19 9
      python/functools.py
  2. 2 2
      python/typing.py

+ 19 - 9
python/functools.py

@@ -1,9 +1,19 @@
-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
+# 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
+
+class cache:
+    def __init__(self, f):
+        self.f = f
+        self.cache = {}
+
+    def __call__(self, *args):
+        if args not in self.cache:
+            self.cache[args] = self.f(*args)
+        return self.cache[args]

+ 2 - 2
python/typing.py

@@ -1,4 +1,4 @@
-class Placeholder:
+class _Placeholder:
     def __init__(self, *args, **kwargs):
         pass
     def __getitem__(self, *args, **kwargs):
@@ -7,7 +7,7 @@ class Placeholder:
         return self
     
 
-_PLACEHOLDER = Placeholder()
+_PLACEHOLDER = _Placeholder()
 
 List = _PLACEHOLDER
 Dict = _PLACEHOLDER