{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "uQ3RXDjQUlwp" }, "source": [ "# Ejemplo ```super()``` ```isinstance()``` ```issubclass()```" ] }, { "cell_type": "markdown", "metadata": { "id": "jmjmEczlUlws" }, "source": [ "## Calculadora" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "F4uL2h4XUlwu" }, "outputs": [], "source": [ "class Calculadora: # clase padre\n", " def __init__(self, numero):\n", " self.n = numero # número de datos que intervienen en la operación\n", " self.datos = [0 for i in range(numero)]\n", " def ingresardato(self):\n", " self.datos = [int(input('Ingresar dato ' + str(i+1) + ' = ')) for i in range(self.n)]\n", "\n", "class OperacionesBasicas(Calculadora): # clase hija\n", " def __init__(self): # sobrescribimos el método constructor\n", " Calculadora.__init__(self, 2) # podríamos poner la función super() y quitar el self\n", " def suma(self):\n", " a,b, = self.datos\n", " return a + b\n", " def resta(self):\n", " a,b, = self.datos\n", " return a - b\n", "\n", "class Raiz(Calculadora): # otra clase hija\n", " def __init__(self): # sobrescribimos el método constructor\n", " super().__init__(1) # aquí si trabajamos con la función super() y hemos quitado el self\n", " def cuadrada(self):\n", " a, = self.datos\n", " return a**.5\n", "\n", "caso1 = OperacionesBasicas() # instanciamos un objeto de la clase OperacionesBasicas que necesita 2 argumentos\n", "caso2 = Raiz() # instanciamos un objeto de la clase Raiz que solo usa un argumento" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "vjSH2AHTUlwx", "outputId": "617c7fa4-3343-4842-d7ea-1597787448d6", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ingresar dato 1 = 2\n", "Ingresar dato 2 = 3\n" ] } ], "source": [ "caso1.ingresardato() # invocamos este método que nos pedirá dos datos por ser de la clase OpBasicas" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "O5jcEAxWUlwy", "outputId": "7f9fedf7-75d6-437c-c1be-f696b6d5f2d2", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "5" ] }, "metadata": {}, "execution_count": 3 } ], "source": [ "caso1.suma() # invocamos el método suma y sumamos los dos datos" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "abzIhZuWUlwz", "outputId": "41e5869f-4d08-4afc-873c-9b19da303f8d", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "-1" ] }, "metadata": {}, "execution_count": 4 } ], "source": [ "caso1.resta() # invocamos el método resta" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "bCwo4SJpUlw0", "outputId": "a23ca9e5-4822-499b-9306-82ced90d4947", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Ingresar dato 1 = 9\n" ] } ], "source": [ "caso2.ingresardato() # invocamos este método que nos pedirá 1 dato por ser de la clase Raiz" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "id": "xhUFBwJfUlw1", "outputId": "07aa0261-0698-4d4a-88ea-37e59654e0fb", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "3.0" ] }, "metadata": {}, "execution_count": 6 } ], "source": [ "caso2.cuadrada() # invocamos el método cuadrada que usa 1 dato y hace la raíz cuadrada" ] }, { "cell_type": "markdown", "metadata": { "id": "DfzGFDqzUlw2" }, "source": [ "### ```isinstance(objeto, clase)```\n", "Retorna True o False según que el objeto pertenezca o no a cierta clase." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "CM7NxbJlUlw3", "outputId": "20f84f0f-8ace-4830-83d3-85bd3e04dacd", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 7 } ], "source": [ "isinstance(caso1, OperacionesBasicas)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "id": "6xjPWCRgUlw4", "outputId": "2d0b3400-a806-4beb-c591-f77f3a1f3510", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 8 } ], "source": [ "isinstance(caso1, Calculadora)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "id": "olsvMNaCUlw5", "outputId": "0b6487a3-0c27-4f8a-dec3-5cc44277e346", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": {}, "execution_count": 9 } ], "source": [ "isinstance(caso1, Raiz)" ] }, { "cell_type": "markdown", "metadata": { "id": "ehGWlJjQUlw5" }, "source": [ "### ```issubclass(Subclase, Clase)```\n", "Retorna True si la subclase hereda de la clase indicadas, en ese orden." ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "id": "EyObj2pdUlw6", "outputId": "3f8a4c96-8bab-4c4f-919c-8c749f4becd4", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 10 } ], "source": [ "issubclass(Raiz, Calculadora) # True. Raiz si es subclase de Calculadora" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "id": "AVq9o0KyUlw6", "outputId": "8886c0d3-71e5-4b6f-f61f-7a404608affe", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "False" ] }, "metadata": {}, "execution_count": 11 } ], "source": [ "issubclass(Raiz, OperacionesBasicas) # False. Raiz no es subclase de OperacionesBasicas" ] } ], "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.5" }, "colab": { "name": "0290_super_isinstance_issubclass.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 0 }