diff --git a/python-ofensivo/00_ejercicios/05_encapsulamiento03.py b/python-ofensivo/00_ejercicios/05_encapsulamiento03.py new file mode 100644 index 0000000..c7b56c0 --- /dev/null +++ b/python-ofensivo/00_ejercicios/05_encapsulamiento03.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +""" +Ejemplo de métodos especiales +""" + + +class Libro: + + def __init__(self, autor, titulo): + + self.titulo = titulo + self.autor = autor + + def __str__(self): + + return f"El libro '{self.titulo}' fue escrito por {self.autor}" + + def __eq__(self, otro): + + return self.titulo == otro.titulo and self.autor == otro.autor + + +libro_uno = Libro("Jose Saramago", "Ensayo sobre la ceguera") +libro_dos = Libro("Jose Saramago", "Ensayo sobre la vista") + +print(libro_uno) +print(libro_dos) + +print(f"¿Son iguales ambos libros? --> {libro_uno == libro_dos}") diff --git a/python-ofensivo/00_ejercicios/05_encapsulamiento04.py b/python-ofensivo/00_ejercicios/05_encapsulamiento04.py new file mode 100644 index 0000000..42ddc11 --- /dev/null +++ b/python-ofensivo/00_ejercicios/05_encapsulamiento04.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 +""" + +""" + +class Caja: + + def __init__(self, *items): + self.items = items + + def __str__(self): + + print("Se ha creado una caja con las siguientes frutas: ") + + for item in self.items: + print(item) + + +caja = Caja("Manzana", "Pera", "Naranja", "Platano") diff --git a/python-ofensivo/00_ejercicios/05_encapsulamiento05.py b/python-ofensivo/00_ejercicios/05_encapsulamiento05.py new file mode 100644 index 0000000..6f40da6 --- /dev/null +++ b/python-ofensivo/00_ejercicios/05_encapsulamiento05.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 +""" +Métodos especiales +""" + + +class Caja: + + def __init__(self, *items): + self.items = items + + def mostrar_items(self): + + print("Se ha creado una caja con las siguientes frutas: ") + + for item in self.items: + print(item) + + def __len__(self): + + return len(self.items) + + +caja = Caja("Manzana", "Pera", "Naranja", "Platano", "Melon") + +caja.mostrar_items() + +print("La caja tiene {} frutas".format(len(caja))) diff --git a/python-ofensivo/00_ejercicios/05_encapsulamiento06.py b/python-ofensivo/00_ejercicios/05_encapsulamiento06.py new file mode 100644 index 0000000..0b92e56 --- /dev/null +++ b/python-ofensivo/00_ejercicios/05_encapsulamiento06.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +""" +Métodos especiales +""" + + +class Pizza: + + def __init__(self, size, *toppings): + + self.size = size + self.toppings = toppings + + def description(self): + + print( + f""" +Esta pizza es de {self.size} cm +y tiene los siguientes ingredientes:""") + for topping in self.toppings: + print(f"- {topping}") + +pizza = Pizza(12, "Chorizo", "Queso", "Jamon", "Cebolla") + +pizza.description() diff --git a/python-ofensivo/00_ejercicios/05_encapsulamiento07.py b/python-ofensivo/00_ejercicios/05_encapsulamiento07.py new file mode 100644 index 0000000..c803efe --- /dev/null +++ b/python-ofensivo/00_ejercicios/05_encapsulamiento07.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 +""" +Métodos especiales +""" + + +class MiLista: + + def __init__(self): + self.data = [1, 2, 3, 4, 5] + + def __getitem__(self, index): + return self.data[index] + +lista = MiLista() + +print(lista[2]) diff --git a/python-ofensivo/00_ejercicios/05_encapsulamiento08.py b/python-ofensivo/00_ejercicios/05_encapsulamiento08.py new file mode 100644 index 0000000..27eb68f --- /dev/null +++ b/python-ofensivo/00_ejercicios/05_encapsulamiento08.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +""" +Métodos especiales +""" + + +class Saludo: + + def __init__(self, saludo): + + self.saludo = saludo + + def __call__(self, nombre): + + return f"{self.saludo} {nombre}!" + + +hola = Saludo("¡Hola") + +print(hola("Luis")) + +print(hola("Juan")) diff --git a/python-ofensivo/00_ejercicios/05_encapsulamiento09.py b/python-ofensivo/00_ejercicios/05_encapsulamiento09.py new file mode 100644 index 0000000..752779c --- /dev/null +++ b/python-ofensivo/00_ejercicios/05_encapsulamiento09.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +""" +Métodos especiales +""" + + +class Punto: + + def __init__(self, x, y): + + self.x = x + self.y = y + + def __add__(self, otro): + + return Punto(self.x + otro.x, self.y + otro.y) + + def __str__(self): + + return f"Punto({self.x},{self.y})" + + +p1 = Punto(2, 8) +p2 = Punto(4, 9) + +print(p1 + p2) # (6, 17) diff --git a/python-ofensivo/00_ejercicios/05_encapsulamiento10.py b/python-ofensivo/00_ejercicios/05_encapsulamiento10.py new file mode 100644 index 0000000..2b44060 --- /dev/null +++ b/python-ofensivo/00_ejercicios/05_encapsulamiento10.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +""" +Métodos especiales +""" + + +class Contador: + + def __init__(self, limite): + + self.limite = limite + + def __iter__(self): + + self.valor = 0 + + return self + + def __next__(self): + + if self.valor < self.limite: + self.valor += 1 + return self.valor + else: + raise StopIteration + +c = Contador(5) + +for i in c: + print(i)