File tree Expand file tree Collapse file tree 1 file changed +56
-0
lines changed
Roadmap/24 - DECORADORES/python Expand file tree Collapse file tree 1 file changed +56
-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+ """
8+
9+ def decorator_operations (function ):
10+ def show_name (* args ):
11+ print (f"Estás utilizando la función { function .__name__ } :" )
12+ return function (* args )
13+ return show_name
14+
15+
16+ @decorator_operations
17+ def sumar (a , b ):
18+ print (f"{ a } + { b } = { a + b } " )
19+
20+ def resta (a , b ):
21+ print (f"{ a } - { b } = { a - b } " )
22+
23+ def multiplica (a , b ):
24+ print (f"{ a } x { b } = { a * b } " )
25+
26+ sumar (2 , 4 )
27+ resta (2 , 4 )
28+ multiplica (2 , 4 )
29+
30+ """
31+ *
32+ * DIFICULTAD EXTRA (opcional):
33+ * Crea un decorador que sea capaz de contabilizar cuántas veces
34+ * se ha llamado a una función y aplícalo a una función de tu elección.
35+ */
36+
37+ """
38+
39+
40+ def func_counter (function ):
41+
42+ def counter (* args ):
43+ counter .call_counter += 1
44+ print (f"La función { function .__name__ } se ha ejecutado { counter .call_counter } veces" )
45+ return function (* args )
46+ counter .call_counter = 0
47+ return counter
48+
49+
50+ @func_counter
51+ def saludo (name ):
52+ print (f"Hola, { name } ¿Cómo estás?" )
53+
54+
55+ for name in ["Brais" , "Borja" , "Sonia" ]:
56+ saludo (name )
You can’t perform that action at this time.
0 commit comments