Browse Source

Cache example.

Fred Damstra 6 years ago
parent
commit
a13803c4c0
3 changed files with 23 additions and 0 deletions
  1. 2 0
      .gitignore
  2. 7 0
      README.md
  3. 14 0
      examples/cache.py

+ 2 - 0
.gitignore

@@ -58,3 +58,5 @@ docs/_build/
 # PyBuilder
 target/
 
+# Vim
+.*.swp

+ 7 - 0
README.md

@@ -2,6 +2,13 @@
 
 Just notes on various things
 
+## Magic Methods
+Classes have certain magic methods that help make objects behave in pythonlike ways.
+
+* `__repr__` returns a string representation of the object. Should be unambiguous. (i.e. add parameters).
+* `__str__` returns a string representation of the object. Should be easily readable but not necessarily
+  unambiguous. Careful, can be confused with `__repr__` in some cases. 
+
 ## docstrings
 Use 'em.
 

+ 14 - 0
examples/cache.py

@@ -0,0 +1,14 @@
+from functools import lru_cache
+
+def fib(x):
+    if x < 2:
+        return 1
+    return fib(x-1) + fib(x-2)
+
+@lru_cache(maxsize=10000)
+def cfib(x):
+    if x < 2:
+        return 1
+    return fib(x-1) + fib(x-2)
+
+