Skip to content

Commit a8f787f

Browse files
committed
#24 - Python
1 parent d9ef3f3 commit a8f787f

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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)

0 commit comments

Comments
 (0)