From cef1ceae1206fdb34892e6642ae8c4b430e3dca1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Tue, 29 Jan 2019 17:22:03 +0100 Subject: [PATCH] Decorator --- README.md | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f2030bb..b72b1e5 100644 --- a/README.md +++ b/README.md @@ -608,11 +608,13 @@ def function_that_gets_passed_to_decorator(): ``` ### Debugger Example +**"Wraps" decorator copies metadata of function func() to function out(). Without it `add.__name__` would return `'out'`.** + ```python from functools import wraps def debug(func): - @wraps(func) # Copies func's metadata like name and doc to out. + @wraps(func) def out(*args, **kwargs): print(func.__name__) return func(*args, **kwargs) @@ -625,6 +627,7 @@ def add(x, y): ### LRU Cache **Decorator that caches function's return values. All arguments must be hashable.** + ```python from functools import lru_cache @@ -634,10 +637,10 @@ def fib(n): ``` ```python ->>> [fib(n) for n in range(16)] -[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610] +>>> [fib(n) for n in range(8)] +[0, 1, 1, 2, 3, 5, 8, 13] >>> fib.cache_info() -CacheInfo(hits=28, misses=16, maxsize=None, currsize=16) +CacheInfo(hits=12, misses=8, maxsize=None, currsize=8) ``` ### Parametrized Decorator