Browse Source

Decorator

pull/10/head
Jure Šorn 6 years ago
parent
commit
bd4bcdc8d3
1 changed files with 7 additions and 5 deletions
  1. 12
      README.md

12
README.md

@ -599,9 +599,11 @@ def get_counter():
Decorator Decorator
--------- ---------
**A decorator takes a function, adds some functionality and returns it.**
```python ```python
@closure_name @decorator_name
def function_that_gets_passed_to_closure(): def function_that_gets_passed_to_decorator():
... ...
``` ```
@ -638,17 +640,17 @@ def fib(n):
CacheInfo(hits=28, misses=16, maxsize=None, currsize=16) CacheInfo(hits=28, misses=16, maxsize=None, currsize=16)
``` ```
### Parametrized Decorator ### Parametrized Example
```python ```python
def debug(print_result=False): def debug(print_result=False):
def inner_decorator(func): def decorator(func):
@wraps(func) @wraps(func)
def out(*args, **kwargs): def out(*args, **kwargs):
result = func(*args, **kwargs) result = func(*args, **kwargs)
print(func.__name__, result if print_result else '') print(func.__name__, result if print_result else '')
return result return result
return out return out
return inner_decorator return decorator
@debug(print_result=True) @debug(print_result=True)
def add(x, y): def add(x, y):

|||||||
100:0
Loading…
Cancel
Save