Skip to content

Commit 922f6c0

Browse files
committed
Decorator
1 parent 201f9aa commit 922f6c0

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,12 +605,12 @@ def function_that_gets_passed_to_closure():
605605
...
606606
```
607607

608-
#### Debugger example:
608+
### Debugger Example
609609
```python
610610
from functools import wraps
611611

612612
def debug(func):
613-
@wraps(func) # Needed for metadata copying (func name, ...).
613+
@wraps(func) # Copies func's metadata like name and doc to out.
614614
def out(*args, **kwargs):
615615
print(func.__name__)
616616
return func(*args, **kwargs)
@@ -638,6 +638,24 @@ def fib(n):
638638
CacheInfo(hits=28, misses=16, maxsize=None, currsize=16)
639639
```
640640

641+
### Parametrized Decorator
642+
```python
643+
def debug(print_result=False):
644+
def inner_decorator(func):
645+
@wraps(func)
646+
def out(*args, **kwargs):
647+
result = func(*args, **kwargs)
648+
print(func.__name__, result if print_result else '')
649+
return result
650+
return out
651+
return inner_decorator
652+
653+
@debug(print_result=True)
654+
def add(x, y):
655+
return x + y
656+
```
657+
658+
641659
Class
642660
-----
643661
```python

0 commit comments

Comments
 (0)