{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "90d072c4", "metadata": { "id": "90d072c4" }, "source": [ "# Ejemplos sin herencia [4]" ] }, { "cell_type": "code", "execution_count": 1, "id": "4e561163", "metadata": { "id": "4e561163" }, "outputs": [], "source": [ "class Cliente: # una clase para los clientes\n", "\n", " def __init__(self, dni, nombre, apellidos):\n", " self.dni = dni\n", " self.nombre = nombre\n", " self.apellidos = apellidos\n", "\n", " def __str__(self):\n", " return f'{self.nombre} {self.apellidos} con DNI {self.dni}.'\n", "\n", "class Empresa: # otra clase para las empresas\n", "\n", " def __init__(self, clientes=[]):\n", " self.clientes = clientes\n", "\n", " def get_cliente(self, dni = None):\n", " for c in self.clientes:\n", " if c.dni == dni:\n", " print(\"El cliente es:\", c) # si no existiera __str__ no funcionaría poner c, sería: c.nombre, c.apellidos\n", " return\n", " print('Cliente no encontrado.')\n", "\n", " def del_cliente(self, dni = None):\n", " for i,c in enumerate(self.clientes):\n", " if c.dni == dni:\n", " del(self.clientes[i])\n", " print(f'{c} → BORRADO') # sin __str__ se tendría que poner: {c.nombre} {c.apellidos}\n", " return\n", " print('Cliente no encontrado.')" ] }, { "cell_type": "code", "execution_count": 2, "id": "0f6db5ad", "metadata": { "id": "0f6db5ad" }, "outputs": [], "source": [ "jose = Cliente(nombre=\"Jose Luis\", apellidos=\"Ruiz\", dni=\"123A\") # creamos un cliente\n", "rober = Cliente(\"444B\", \"Roberto\", \"López\") # creamos otro cliente\n", "empresa =Empresa(clientes=[jose, rober]) # creamos una empresa con los clientes" ] }, { "cell_type": "code", "execution_count": 3, "id": "eaf3d7c8", "metadata": { "id": "eaf3d7c8", "outputId": "2e974092-4d62-418e-d952-8e8febab3c8f", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[<__main__.Cliente at 0x7d2e853fe440>, <__main__.Cliente at 0x7d2e853ff400>]" ] }, "metadata": {}, "execution_count": 3 } ], "source": [ "empresa.clientes # listado de clientes, vemos solo los objetos con la dirección de memoria" ] }, { "cell_type": "code", "execution_count": 4, "id": "b8f113b9", "metadata": { "id": "b8f113b9", "outputId": "924522d3-0b6d-4da9-d30c-e1c69247723e", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "El cliente es: Jose Luis Ruiz con DNI 123A.\n" ] } ], "source": [ "empresa.get_cliente(\"123A\") # consultamos un cliente por dni" ] }, { "cell_type": "code", "execution_count": 5, "id": "447e26f1", "metadata": { "id": "447e26f1", "outputId": "cd2d3d55-bb1a-4e08-b26c-b8822cd38001", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Jose Luis Ruiz con DNI 123A. → BORRADO\n" ] }, { "output_type": "execute_result", "data": { "text/plain": [ "[<__main__.Cliente at 0x7d2e853ff400>]" ] }, "metadata": {}, "execution_count": 5 } ], "source": [ "empresa.del_cliente(\"123A\") # borro un cliente por dni\n", "empresa.clientes # muestro de nuevo todos los clientes" ] }, { "cell_type": "code", "execution_count": 6, "id": "cba63133", "metadata": { "id": "cba63133", "outputId": "bddd5cf2-fa3e-4b44-ecf0-3f4babeacbb7", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Cliente no encontrado.\n" ] } ], "source": [ "empresa.del_cliente(\"123Z\") # borro un cliente con dni inexistente" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" }, "colab": { "name": "calisto2_0190.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }