File tree Expand file tree Collapse file tree 1 file changed +20
-2
lines changed Expand file tree Collapse file tree 1 file changed +20
-2
lines changed Original file line number Diff line number Diff line change @@ -605,12 +605,12 @@ def function_that_gets_passed_to_closure():
605
605
...
606
606
```
607
607
608
- #### Debugger example:
608
+ ### Debugger Example
609
609
``` python
610
610
from functools import wraps
611
611
612
612
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.
614
614
def out (* args , ** kwargs ):
615
615
print (func.__name__ )
616
616
return func(* args, ** kwargs)
@@ -638,6 +638,24 @@ def fib(n):
638
638
CacheInfo(hits = 28 , misses = 16 , maxsize = None , currsize = 16 )
639
639
```
640
640
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
+
641
659
Class
642
660
-----
643
661
``` python
You can’t perform that action at this time.
0 commit comments