File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
Roadmap/24 - DECORADORES/python Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ #Basic decorator
2
+ def my_decorator (function ):
3
+ def my_wrapper (* args ,** kwargs ):
4
+ print ("Initialize wrapper" )
5
+ result = function (* args ,** kwargs )
6
+ print ("Finishing wrapper" )
7
+ return result
8
+
9
+ return my_wrapper
10
+
11
+ @my_decorator
12
+ def suma (a ,b ):
13
+ return a + b
14
+
15
+ print (suma (2 ,3 ))
16
+
17
+ #Extra
18
+ def my_decorator2 (function ):
19
+ execution = 0
20
+ def my_wrapper2 (* args ,** kwargs ):
21
+ nonlocal execution
22
+ execution += 1
23
+ function (* args ,** kwargs )
24
+ return execution
25
+ return my_wrapper2
26
+
27
+ @my_decorator2
28
+ def multiply (value1 ,value2 ):
29
+ print (f'Value of multiplication { value1 * value2 } ' )
30
+
31
+ print (f'Number of executions { multiply (4 ,2 )} ' )
32
+ print (f'Number of executions { multiply (8 ,7 )} ' )
You can’t perform that action at this time.
0 commit comments