{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "711e4d5d", "metadata": { "id": "711e4d5d" }, "source": [ "# Ejemplos sin herencia [1]" ] }, { "cell_type": "markdown", "id": "795a5313", "metadata": { "id": "795a5313" }, "source": [ "## La clase Gato" ] }, { "cell_type": "code", "execution_count": 1, "id": "29d95ce7", "metadata": { "id": "29d95ce7" }, "outputs": [], "source": [ "class Gato:\n", " especie = \"Mamífero\" # atributo de clase, se usa para atributos constantes para todos los objetos\n", "\n", " def __init__(self, nombre, edad):\n", " self.nombre = nombre # los atributos que están dentro del constructor son atributos de instancia\n", " self.edad = edad # eso supone que cada objeto instanciado tiene sus propios atributos\n", " self.alimentos = [] # un gato puede tener una edad, otro otra, o diferentes alimentos favoritos\n", " def verEtapa(self):\n", " if self.edad > 1:\n", " print(self.nombre, \"es adulto\")\n", " else:\n", " print(self.nombre, \"es cachorro\")\n", " def esAlimento(self, alimento):\n", " return alimento in self.alimentos" ] }, { "cell_type": "markdown", "id": "21674d09", "metadata": { "id": "21674d09" }, "source": [ "### Instanciando objetos y comprobando métodos" ] }, { "cell_type": "code", "execution_count": 2, "id": "969dacda", "metadata": { "id": "969dacda", "outputId": "5b60b120-cf86-4a29-d773-5dd9c449e08f", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Gufi es cachorro\n" ] } ], "source": [ "g = Gato('Gufi', 0.5)\n", "g.verEtapa()" ] }, { "cell_type": "code", "execution_count": 3, "id": "9ce5e38b", "metadata": { "id": "9ce5e38b", "outputId": "1605abc5-ef8d-4204-e626-a77f967d5079", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Mafi es adulto\n" ] } ], "source": [ "m = Gato('Mafi', 3)\n", "m.verEtapa()" ] }, { "cell_type": "markdown", "id": "9221d70c", "metadata": { "id": "9221d70c" }, "source": [ "### Lista de cada objeto\n", "La lista de alimentos favoritos es propia de cada objeto gato." ] }, { "cell_type": "code", "execution_count": 4, "id": "3635fb13", "metadata": { "id": "3635fb13" }, "outputs": [], "source": [ "g.alimentos.append(\"Sardinas\") # como es una lista puedo usar append" ] }, { "cell_type": "code", "execution_count": 5, "id": "23ead2b8", "metadata": { "id": "23ead2b8", "outputId": "72ce0c9a-6667-443b-9999-d4d9467c0a54", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Sardinas']" ] }, "metadata": {}, "execution_count": 5 } ], "source": [ "g.alimentos" ] }, { "cell_type": "code", "execution_count": 6, "id": "9d03e717", "metadata": { "id": "9d03e717", "outputId": "2acb8e24-c8a2-48bc-8106-b7e3ef4bc06c", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 6 } ], "source": [ "g.esAlimento(\"Sardinas\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "c73bc9f9", "metadata": { "id": "c73bc9f9" }, "outputs": [], "source": [ "m.alimentos.append(\"Natillas\") # al otro gato le gustan las natillas\n", "g.alimentos.append(\"Leche\") # al cachorro le gusta la leche" ] }, { "cell_type": "code", "execution_count": 8, "id": "5c772882", "metadata": { "id": "5c772882", "outputId": "e3c38908-dbf2-4965-e455-d3d35f96e5a4", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "['Sardinas', 'Leche']\n", "['Natillas']\n" ] } ], "source": [ "print(g.alimentos)\n", "print(m.alimentos)" ] }, { "cell_type": "code", "execution_count": 9, "id": "8ad7ce37", "metadata": { "id": "8ad7ce37", "outputId": "6450fef8-f6d9-43bd-c4ee-0014d3f38cd9", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": {}, "execution_count": 9 } ], "source": [ "g.esAlimento(\"Horchata\")" ] }, { "cell_type": "code", "execution_count": 10, "id": "7c9ba0fb", "metadata": { "id": "7c9ba0fb", "outputId": "0cce8009-bfbb-4e58-96b2-92d3a3273aa2", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Gufi es adulto\n" ] } ], "source": [ "g.edad += 1 # podemos acceder y modificar los atributos de instancia\n", "g.verEtapa()" ] }, { "cell_type": "markdown", "id": "767d908a", "metadata": { "id": "767d908a" }, "source": [ "### Los atributos de clase se denominan \"estáticos\"\n", "No es necesario un objeto para obtenerlos. Podemos poner Gato.especie y funciona." ] }, { "cell_type": "code", "execution_count": 11, "id": "b6c2bb6f", "metadata": { "id": "b6c2bb6f", "outputId": "2e4e88b1-e9cf-44ff-fa12-4888e6c69430", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Mamífero'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 11 } ], "source": [ "m.especie" ] }, { "cell_type": "code", "execution_count": 12, "id": "644b892f", "metadata": { "id": "644b892f", "outputId": "2c60c87a-31cf-4ad4-e15e-0eb1e75cb88a", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Mamífero'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 12 } ], "source": [ "g.especie" ] }, { "cell_type": "code", "execution_count": 13, "id": "7a4c18de", "metadata": { "id": "7a4c18de", "outputId": "5c532ccd-7d47-4342-d675-4a40f2f7a810", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Mamífero'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 13 } ], "source": [ "Gato.especie # con los atributos de clase podemos poner la clase y el atributo y funciona" ] }, { "cell_type": "markdown", "id": "6a354a7e", "metadata": { "id": "6a354a7e" }, "source": [ "### Atributos creados de forma dinámica\n", "Podemos crear atributos fuera de la clase, de forma dinámica, pero no se guardarán en la clase y cuando se reinicie el *kernel* no se recordarán." ] }, { "cell_type": "code", "execution_count": 14, "id": "8a3371de", "metadata": { "id": "8a3371de", "outputId": "6d99c69c-8bd2-46b1-ed8a-c155a44349b5", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Siamés'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 14 } ], "source": [ "g.raza = \"Siamés\" # en Python los atributos se pueden crear de forma dinámica\n", "g.raza # podemos consultar el atributo, pero no se guarda" ] }, { "cell_type": "code", "execution_count": 15, "id": "973bb814", "metadata": { "id": "973bb814", "outputId": "72b4a3e4-222b-4145-b57c-9a364da469ec", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'suave'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 15 } ], "source": [ "Gato.pelaje = \"suave\" # también funciona con los atributos de clase, nos inventamos una nuevo\n", "Gato.pelaje # podemos consultar el atributo inventado, pero no se guardará" ] } ], "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_0160.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }