File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
Roadmap/24 - DECORADORES/python Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ /*
3
+ * EJERCICIO:
4
+ * Explora el concepto de "decorador" y muestra cómo crearlo
5
+ * con un ejemplo genérico.
6
+ *
7
+ * DIFICULTAD EXTRA (opcional):
8
+ * Crea un decorador que sea capaz de contabilizar cuántas veces
9
+ * se ha llamado a una función y aplícalo a una función de tu elección.
10
+ */
11
+ """
12
+ # EJERCICIO:
13
+
14
+
15
+ def mi_decorador (fn ):
16
+ def wrapper (* args , ** kwargs ):
17
+ print ("Mi decorador..." )
18
+ return fn (* args , ** kwargs )
19
+ return wrapper
20
+
21
+
22
+ @mi_decorador
23
+ def suma (a , b ):
24
+ return a + b
25
+
26
+
27
+ print (suma (10 , 20 ))
28
+
29
+ # DIFICULTAD EXTRA:
30
+
31
+
32
+ def counter (fn ):
33
+ def wrapper (* args , ** kwargs ):
34
+ wrapper .count += 1
35
+ return fn (* args , ** kwargs )
36
+ wrapper .count = 0
37
+ return wrapper
38
+
39
+
40
+ @counter
41
+ def resta ():
42
+ return 10 - 5
43
+
44
+
45
+ resta ()
46
+ resta ()
47
+ resta ()
48
+ print (resta .count )
You can’t perform that action at this time.
0 commit comments