Last active
August 28, 2023 23:31
-
-
Save Scot3004/5d57573d97ed859afc5dd662556bf14c to your computer and use it in GitHub Desktop.
poo_en_python_with_plant.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/Scot3004/5d57573d97ed859afc5dd662556bf14c/poo_en_python_with_plant.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "FaROxeyg3HRi" | |
}, | |
"source": [ | |
"# Python orientado a objetos\n", | |
"\n", | |
"Hola, El dia de hoy vamos a estar aprendiendo a programar en **python** usando **jupyter notebook**, e instalaremos python en nuestras maquinas, de momento no nos preocuparemos por entornos virtuales y nos apoyaremos en la instalación que tenga nuestro sistema operativo\n", | |
"\n", | |
"El objetivo del dia de hoy es aprender sobre la creación de objetos en python" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "3RqEWyvy3HRm" | |
}, | |
"source": [ | |
"# Instalación de python en windows\n", | |
"\n", | |
"Colocar captura de como instale python en mi pc" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "kYnYXuge3HRm" | |
}, | |
"source": [ | |
"# Python en VS code\n", | |
"\n", | |
"Visual studio code es un editor creado por Microsoft el cual permite el uso de extensiones para facilitar el desarrollo.\n", | |
"Entre las extensiones que podemos instalar en VS Code están las extensiones de python y jupyter\n", | |
"\n", | |
"Python: https://marketplace.visualstudio.com/items?itemName=ms-python.python\n", | |
"Jupyter: https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter\n", | |
"\n", | |
"Recomiendo la instalación de ambas incluyendo el paquete de extensiones de jupyter que trae los renderers para la salida de los notebooks\n", | |
"https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter-renderers\n", | |
"\n", | |
"\n", | |
"\n", | |
"Si descargamos python desde microsoft store debemos adicionar la ruta donde se instala python al path, en mi caso es `C:\\Users\\sergio.orozcot\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python310\\Scripts`\n", | |
"\n", | |
"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "qZMwMMAy-hkn" | |
}, | |
"source": [ | |
"Para esta sesión nos apoyaremos en plant uml entonces por favor ejecutar la siguiente linea" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "gXoJAuvO-gvS", | |
"outputId": "5b290a57-9085-4f5a-e8ae-4f15754b1c66" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", | |
"Collecting iplantuml\n", | |
" Downloading IPlantUML-0.1.1.tar.gz (5.5 kB)\n", | |
"Collecting plantweb\n", | |
" Downloading plantweb-1.2.1-py3-none-any.whl (20 kB)\n", | |
"Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from plantweb->iplantuml) (1.15.0)\n", | |
"Requirement already satisfied: docutils in /usr/local/lib/python3.7/dist-packages (from plantweb->iplantuml) (0.17.1)\n", | |
"Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from plantweb->iplantuml) (2.23.0)\n", | |
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests->plantweb->iplantuml) (2022.6.15)\n", | |
"Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests->plantweb->iplantuml) (2.10)\n", | |
"Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests->plantweb->iplantuml) (1.24.3)\n", | |
"Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests->plantweb->iplantuml) (3.0.4)\n", | |
"Building wheels for collected packages: iplantuml\n", | |
" Building wheel for iplantuml (setup.py) ... \u001b[?25l\u001b[?25hdone\n", | |
" Created wheel for iplantuml: filename=IPlantUML-0.1.1-py2.py3-none-any.whl size=4910 sha256=976a41688f4de34923b643fe30cd57437ba613724d7e52558dc2ea135b53eef0\n", | |
" Stored in directory: /root/.cache/pip/wheels/cf/64/08/5bac65794ab011a60f7ef62413d3c430cf715345028f4b3914\n", | |
"Successfully built iplantuml\n", | |
"Installing collected packages: plantweb, iplantuml\n", | |
"Successfully installed iplantuml-0.1.1 plantweb-1.2.1\n" | |
] | |
} | |
], | |
"source": [ | |
"!pip install iplantuml" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"id": "3EjbYLifDgnm" | |
}, | |
"outputs": [], | |
"source": [ | |
"import iplantuml" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "UzdmSi7A3HRn" | |
}, | |
"source": [ | |
"# Que la programación orientada a objetos\n", | |
"\n", | |
"Es un paradigma de programación que parte del concepto de \"objetos\" como base, los cuales contienen información en forma de campos (a veces también referidos como atributos o propiedades) y código en forma de métodos." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"id": "fuD1g5V8-rKq", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 107 | |
}, | |
"outputId": "ca54441a-03c1-4f76-cb3a-1bc535d8691b" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"<IPython.core.display.SVG object>" | |
], | |
"image/svg+xml": "<svg contentStyleType=\"text/css\" height=\"85px\" preserveAspectRatio=\"none\" style=\"width:92px;height:85px;background:#FFFFFF;\" version=\"1.1\" viewBox=\"0 0 92 85\" width=\"92px\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" zoomAndPan=\"magnify\"><defs/><g><!--MD5=[838fb693084ebcda77f8479951cb9fdf]\nclass Perro--><g id=\"elem_Perro\"><rect fill=\"#F1F1F1\" height=\"64.2969\" id=\"Perro\" rx=\"2.5\" ry=\"2.5\" style=\"stroke:#181818;stroke-width:0.5;\" width=\"71\" x=\"7\" y=\"7\"/><ellipse cx=\"22\" cy=\"23\" fill=\"#ADD1B2\" rx=\"11\" ry=\"11\" style=\"stroke:#181818;stroke-width:1.0;\"/><path d=\"M24.3438,18.6719 C23.4063,18.2344 22.8125,18.0938 21.9375,18.0938 C19.3125,18.0938 17.3125,20.1719 17.3125,22.8906 L17.3125,24.0156 C17.3125,26.5938 19.4219,28.4844 22.3125,28.4844 C23.5313,28.4844 24.6875,28.1875 25.4375,27.6406 C26.0156,27.2344 26.3438,26.7813 26.3438,26.3906 C26.3438,25.9375 25.9531,25.5469 25.4844,25.5469 C25.2656,25.5469 25.0625,25.625 24.875,25.8125 C24.4219,26.2969 24.4219,26.2969 24.2344,26.3906 C23.8125,26.6563 23.125,26.7813 22.3594,26.7813 C20.3125,26.7813 19.0156,25.6875 19.0156,23.9844 L19.0156,22.8906 C19.0156,21.1094 20.2656,19.7969 22,19.7969 C22.5781,19.7969 23.1875,19.9531 23.6563,20.2031 C24.1406,20.4844 24.3125,20.7031 24.4063,21.1094 C24.4688,21.5156 24.5,21.6406 24.6406,21.7656 C24.7813,21.9063 25.0156,22.0156 25.2344,22.0156 C25.5,22.0156 25.7656,21.875 25.9375,21.6563 C26.0469,21.5 26.0781,21.3125 26.0781,20.8906 L26.0781,19.4688 C26.0781,19.0313 26.0625,18.9063 25.9688,18.75 C25.8125,18.4844 25.5313,18.3438 25.2344,18.3438 C24.9375,18.3438 24.7344,18.4375 24.5156,18.75 L24.3438,18.6719 Z \" fill=\"#000000\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"39\" x=\"36\" y=\"27.8467\">Perro</text><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"8\" x2=\"77\" y1=\"39\" y2=\"39\"/><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"8\" x2=\"77\" y1=\"47\" y2=\"47\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"51\" x=\"13\" y=\"63.9951\">ladrar()</text></g><!--MD5=[840edd25c241ac881d81ba89d8e70a4e]\n@startuml\nPerro : ladrar()\n@enduml\n\nPlantUML version 1.2022.7beta2(Unknown compile time)\n(GPL source distribution)\nJava Runtime: Java(TM) SE Runtime Environment\nJVM: Java HotSpot(TM) 64-Bit Server VM\nDefault Encoding: UTF-8\nLanguage: en\nCountry: US\n--></g></svg>" | |
}, | |
"metadata": {}, | |
"execution_count": 3 | |
} | |
], | |
"source": [ | |
"%%plantuml\n", | |
"\n", | |
"@startuml\n", | |
"Perro : ladrar()\n", | |
"@enduml" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "5Ii3_JeW3HRn", | |
"outputId": "e8703747-877b-46fa-de2d-8847f7f21c02" | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"guau guau!\n" | |
] | |
} | |
], | |
"source": [ | |
"class Perro:\n", | |
" def ladrar(self):\n", | |
" print(\"guau guau!\")\n", | |
"\n", | |
"perro = Perro()\n", | |
"perro.ladrar()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "SKYyByo-3HRp" | |
}, | |
"source": [ | |
"Python es un lenguaje donde toca ser explicitos, y en cada metodo de clase es necesario agregar la referencia al objeto que por convención en python llamamos `self`, lo que en algunos lenguajes de progamación llamariamos `this`" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "2pV1bwnX3HRp", | |
"outputId": "ae9c2f01-5618-4f5a-bb3e-f5aa9bd652bb" | |
}, | |
"outputs": [ | |
{ | |
"ename": "TypeError", | |
"evalue": "Perro1.ladrar() takes 0 positional arguments but 1 was given", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[1;32md:\\intro python\\poo_en_python_with_plant.ipynb Cell 10'\u001b[0m in \u001b[0;36m<cell line: 6>\u001b[1;34m()\u001b[0m\n\u001b[0;32m <a href='vscode-notebook-cell:/d%3A/intro%20python/poo_en_python_with_plant.ipynb#ch0000009?line=2'>3</a>\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mguau guau!\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m <a href='vscode-notebook-cell:/d%3A/intro%20python/poo_en_python_with_plant.ipynb#ch0000009?line=4'>5</a>\u001b[0m perro \u001b[39m=\u001b[39m Perro1()\n\u001b[1;32m----> <a href='vscode-notebook-cell:/d%3A/intro%20python/poo_en_python_with_plant.ipynb#ch0000009?line=5'>6</a>\u001b[0m perro\u001b[39m.\u001b[39;49mladrar()\n", | |
"\u001b[1;31mTypeError\u001b[0m: Perro1.ladrar() takes 0 positional arguments but 1 was given" | |
] | |
} | |
], | |
"source": [ | |
"class Perro1:\n", | |
" def ladrar():\n", | |
" print(\"guau guau!\")\n", | |
"\n", | |
"perro = Perro1()\n", | |
"perro.ladrar()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "KMIyZVg23HRp" | |
}, | |
"source": [ | |
"# Metodos estaticos\n", | |
"\n", | |
"Python soporta metodos estaticos. En python como tal para los utilitarios no necesitamos encapsularlo dentro de clases, ya que de por si las funciones son objetos pero si queremos lo podemos hacer." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "dGo-4PtA3HRq", | |
"outputId": "e232cf16-c1c1-413a-9af6-a2d66bfb37cc" | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"algo\n" | |
] | |
} | |
], | |
"source": [ | |
"class Util:\n", | |
" @staticmethod\n", | |
" def hacer_algo():\n", | |
" print(\"algo\")\n", | |
"\n", | |
"Util.hacer_algo()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "5XknMAP73HRq" | |
}, | |
"source": [ | |
"# Metodos magicos\n", | |
"\n", | |
"Son metodos especiales definidos por python y estan prefijados con `__`" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "9G38NXK83HRr", | |
"outputId": "83a61772-305d-4832-a7d8-76681033bd03" | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"6\n", | |
"25\n" | |
] | |
} | |
], | |
"source": [ | |
"class Specialstring:\n", | |
" def __len__(self):\n", | |
" return 25\n", | |
"\n", | |
"normal_string = \"Sergio\"\n", | |
"print(len(normal_string))\n", | |
"\n", | |
"string = Specialstring()\n", | |
"print(len(string))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "mIFxKdpd3HRr" | |
}, | |
"source": [ | |
"# Atributos de clases\n", | |
"\n", | |
"Los atributos de clase en python normalmente se definen en el constructor `__init__` y en dicho metodo se coloca el tipo de datos en caso de usar type hints" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "Ii8OIdgHDgnr", | |
"outputId": "09519e4c-8f4f-4ec2-e37a-376b3be81966" | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"image/svg+xml": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" contentStyleType=\"text/css\" height=\"134px\" preserveAspectRatio=\"none\" style=\"width:128px;height:134px;background:#FFFFFF;\" version=\"1.1\" viewBox=\"0 0 128 134\" width=\"128px\" zoomAndPan=\"magnify\"><defs/><g><!--MD5=[8f63dadf5c643e74a34e53c8f0a15d23]\nclass Persona--><g id=\"elem_Persona\"><rect fill=\"#F1F1F1\" height=\"113.1875\" id=\"Persona\" rx=\"2.5\" ry=\"2.5\" style=\"stroke:#181818;stroke-width:0.5;\" width=\"107\" x=\"7\" y=\"7\"/><ellipse cx=\"30.1\" cy=\"23\" fill=\"#ADD1B2\" rx=\"11\" ry=\"11\" style=\"stroke:#181818;stroke-width:1.0;\"/><path d=\"M32.4438,18.6719 C31.5063,18.2344 30.9125,18.0938 30.0375,18.0938 C27.4125,18.0938 25.4125,20.1719 25.4125,22.8906 L25.4125,24.0156 C25.4125,26.5938 27.5219,28.4844 30.4125,28.4844 C31.6313,28.4844 32.7875,28.1875 33.5375,27.6406 C34.1156,27.2344 34.4438,26.7813 34.4438,26.3906 C34.4438,25.9375 34.0531,25.5469 33.5844,25.5469 C33.3656,25.5469 33.1625,25.625 32.975,25.8125 C32.5219,26.2969 32.5219,26.2969 32.3344,26.3906 C31.9125,26.6563 31.225,26.7813 30.4594,26.7813 C28.4125,26.7813 27.1156,25.6875 27.1156,23.9844 L27.1156,22.8906 C27.1156,21.1094 28.3656,19.7969 30.1,19.7969 C30.6781,19.7969 31.2875,19.9531 31.7563,20.2031 C32.2406,20.4844 32.4125,20.7031 32.5063,21.1094 C32.5688,21.5156 32.6,21.6406 32.7406,21.7656 C32.8813,21.9063 33.1156,22.0156 33.3344,22.0156 C33.6,22.0156 33.8656,21.875 34.0375,21.6563 C34.1469,21.5 34.1781,21.3125 34.1781,20.8906 L34.1781,19.4688 C34.1781,19.0313 34.1625,18.9063 34.0688,18.75 C33.9125,18.4844 33.6313,18.3438 33.3344,18.3438 C33.0375,18.3438 32.8344,18.4375 32.6156,18.75 L32.4438,18.6719 Z \" fill=\"#000000\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"57\" x=\"45.9\" y=\"27.8467\">Persona</text><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"8\" x2=\"113\" y1=\"39\" y2=\"39\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"57\" x=\"13\" y=\"55.9951\">nombre</text><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"57\" x=\"13\" y=\"72.292\">apellido</text><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"8\" x2=\"113\" y1=\"79.5938\" y2=\"79.5938\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"84\" x=\"13\" y=\"96.5889\">__str__(self)</text><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"95\" x=\"13\" y=\"112.8857\">__repr__(self)</text></g><!--MD5=[e2261d8b661a98c419189cc2f7f4b676]\n@startuml\nPersona : nombre\nPersona : apellido\n\nPersona : __str__(self)\nPersona : __repr__(self)\n@enduml\n\nPlantUML version 1.2022.7beta2(Unknown compile time)\n(GPL source distribution)\nJava Runtime: Java(TM) SE Runtime Environment\nJVM: Java HotSpot(TM) 64-Bit Server VM\nDefault Encoding: UTF-8\nLanguage: en\nCountry: US\n--></g></svg>", | |
"text/plain": [ | |
"<IPython.core.display.SVG object>" | |
] | |
}, | |
"execution_count": 28, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%%plantuml\n", | |
"\n", | |
"@startuml\n", | |
"Persona : nombre\n", | |
"Persona : apellido\n", | |
"\n", | |
"Persona : __str__(self)\n", | |
"Persona : __repr__(self)\n", | |
"@enduml" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "AHX0ZMuq3HRr", | |
"outputId": "5b38becf-7dc0-445d-9c18-e7120bee4525" | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Sergio Orozco\n" | |
] | |
} | |
], | |
"source": [ | |
"class Persona:\n", | |
" def __init__(self, nombre, apellido):\n", | |
" self.nombre = nombre\n", | |
" self.apellido = apellido\n", | |
" \n", | |
" def __str__(self):\n", | |
" return \"{} {}\".format(self.nombre ,self.apellido)\n", | |
"\n", | |
" def __repr__(self):\n", | |
" return \"Persona('{}', '{}')\".format(self.nombre, self.apellido)\n", | |
"\n", | |
"sergio = Persona(\"Sergio\", \"Orozco\")\n", | |
"\n", | |
"print(sergio)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "f8FqV3g93HRs" | |
}, | |
"source": [ | |
"# Sobrecarga de operadores\n", | |
"\n", | |
"La sobrecarga, en el contexto de la programación, se refiere a la capacidad de una función o de un operador de comportarse de diferentes maneras dependiendo de los parámetros que se pasan a la función o de los operandos sobre los que actúa el operador.\n", | |
"Por ejemplo podemos definir la suma de 2 Puntos" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "5t0tkmR7Dgns", | |
"outputId": "eb630e1c-7d28-46d2-8ea0-ad8f1b28797c" | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"image/svg+xml": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" contentStyleType=\"text/css\" height=\"150px\" preserveAspectRatio=\"none\" style=\"width:128px;height:150px;background:#FFFFFF;\" version=\"1.1\" viewBox=\"0 0 128 150\" width=\"128px\" zoomAndPan=\"magnify\"><defs/><g><!--MD5=[d4f042c1080df559351a114b3e747b8d]\nclass Punto--><g id=\"elem_Punto\"><rect fill=\"#F1F1F1\" height=\"129.4844\" id=\"Punto\" rx=\"2.5\" ry=\"2.5\" style=\"stroke:#181818;stroke-width:0.5;\" width=\"107\" x=\"7\" y=\"7\"/><ellipse cx=\"36.85\" cy=\"23\" fill=\"#ADD1B2\" rx=\"11\" ry=\"11\" style=\"stroke:#181818;stroke-width:1.0;\"/><path d=\"M39.1938,18.6719 C38.2563,18.2344 37.6625,18.0938 36.7875,18.0938 C34.1625,18.0938 32.1625,20.1719 32.1625,22.8906 L32.1625,24.0156 C32.1625,26.5938 34.2719,28.4844 37.1625,28.4844 C38.3813,28.4844 39.5375,28.1875 40.2875,27.6406 C40.8656,27.2344 41.1938,26.7813 41.1938,26.3906 C41.1938,25.9375 40.8031,25.5469 40.3344,25.5469 C40.1156,25.5469 39.9125,25.625 39.725,25.8125 C39.2719,26.2969 39.2719,26.2969 39.0844,26.3906 C38.6625,26.6563 37.975,26.7813 37.2094,26.7813 C35.1625,26.7813 33.8656,25.6875 33.8656,23.9844 L33.8656,22.8906 C33.8656,21.1094 35.1156,19.7969 36.85,19.7969 C37.4281,19.7969 38.0375,19.9531 38.5063,20.2031 C38.9906,20.4844 39.1625,20.7031 39.2563,21.1094 C39.3188,21.5156 39.35,21.6406 39.4906,21.7656 C39.6313,21.9063 39.8656,22.0156 40.0844,22.0156 C40.35,22.0156 40.6156,21.875 40.7875,21.6563 C40.8969,21.5 40.9281,21.3125 40.9281,20.8906 L40.9281,19.4688 C40.9281,19.0313 40.9125,18.9063 40.8188,18.75 C40.6625,18.4844 40.3813,18.3438 40.0844,18.3438 C39.7875,18.3438 39.5844,18.4375 39.3656,18.75 L39.1938,18.6719 Z \" fill=\"#000000\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"42\" x=\"54.15\" y=\"27.8467\">Punto</text><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"8\" x2=\"113\" y1=\"39\" y2=\"39\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"8\" x=\"13\" y=\"55.9951\">x</text><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"8\" x=\"13\" y=\"72.292\">y</text><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"8\" x2=\"113\" y1=\"79.5938\" y2=\"79.5938\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"90\" x=\"13\" y=\"96.5889\">__add__(self)</text><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"84\" x=\"13\" y=\"112.8857\">__str__(self)</text><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"95\" x=\"13\" y=\"129.1826\">__repr__(self)</text></g><!--MD5=[a2a9e9ce8b16f0d8f67d0fb75ca726b9]\n@startuml\nPunto : x\nPunto : y\nPunto : __add__(self)\nPunto : __str__(self)\nPunto : __repr__(self)\n@enduml\n\nPlantUML version 1.2022.7beta2(Unknown compile time)\n(GPL source distribution)\nJava Runtime: Java(TM) SE Runtime Environment\nJVM: Java HotSpot(TM) 64-Bit Server VM\nDefault Encoding: UTF-8\nLanguage: en\nCountry: US\n--></g></svg>", | |
"text/plain": [ | |
"<IPython.core.display.SVG object>" | |
] | |
}, | |
"execution_count": 27, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%%plantuml\n", | |
"\n", | |
"@startuml\n", | |
"Punto : x\n", | |
"Punto : y\n", | |
"Punto : __add__(self)\n", | |
"Punto : __str__(self)\n", | |
"Punto : __repr__(self)\n", | |
"@enduml" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "bmAMLbMQ3HRs", | |
"outputId": "5a1431a6-2595-4bd7-b77d-c3281254947c" | |
}, | |
"outputs": [ | |
{ | |
"ename": "TypeError", | |
"evalue": "unsupported operand type(s) for +: 'Punto' and 'Punto'", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[1;32md:\\intro python\\poo-en-python.ipynb Cell 16'\u001b[0m in \u001b[0;36m<cell line: 9>\u001b[1;34m()\u001b[0m\n\u001b[0;32m <a href='vscode-notebook-cell:/d%3A/intro%20python/poo-en-python.ipynb#ch0000015?line=5'>6</a>\u001b[0m p1 \u001b[39m=\u001b[39m Punto(\u001b[39m1\u001b[39m, \u001b[39m2\u001b[39m)\n\u001b[0;32m <a href='vscode-notebook-cell:/d%3A/intro%20python/poo-en-python.ipynb#ch0000015?line=6'>7</a>\u001b[0m p2 \u001b[39m=\u001b[39m Punto(\u001b[39m3\u001b[39m, \u001b[39m4\u001b[39m)\n\u001b[1;32m----> <a href='vscode-notebook-cell:/d%3A/intro%20python/poo-en-python.ipynb#ch0000015?line=8'>9</a>\u001b[0m \u001b[39mprint\u001b[39m(p1\u001b[39m+\u001b[39;49mp2)\n", | |
"\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'Punto' and 'Punto'" | |
] | |
} | |
], | |
"source": [ | |
"class Punto:\n", | |
" def __init__(self, x, y):\n", | |
" self.x = x\n", | |
" self.y = y\n", | |
"\n", | |
"p1 = Punto(1, 2)\n", | |
"p2 = Punto(3, 4)\n", | |
"\n", | |
"print(p1+p2) " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "r_EqbZjP3HRs", | |
"outputId": "e73d84ec-9ac7-44b0-829a-8e4a83376d30" | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Punto(8,6)\n", | |
"Punto(2,-2)\n" | |
] | |
} | |
], | |
"source": [ | |
"class Punto:\n", | |
" def __init__(self, x, y):\n", | |
" self.x = x\n", | |
" self.y = y\n", | |
" def __add__(self, other):\n", | |
" return Punto(self.x + other.x, self.y + other.y)\n", | |
" def __sub__(self, other):\n", | |
" return Punto(self.x - other.x, self.y - other.y)\n", | |
" def __repr__(self):\n", | |
" return \"Punto({},{})\".format(self.x, self.y)\n", | |
"\n", | |
"p1 = Punto(5, 2)\n", | |
"p2 = Punto(3, 4)\n", | |
"\n", | |
"print(p1 + p2)\n", | |
"print(p1 - p2)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "00NtQu-O3HRs" | |
}, | |
"source": [ | |
"# Sobrecarga del operador + en colecciones\n", | |
"\n", | |
"En las colecciónes el operador + se usa para crear una nueva lista uniendo los elementos de ambas listas" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "nbWoRPO73HRs", | |
"outputId": "d6a3bcdf-a8fc-4bdf-d4df-f57300d05f7d" | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(3, 4, 2, 6)" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"(3,4)+(2,6)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "PGnqiACk3HRt", | |
"outputId": "3943d40e-413e-446e-a7e7-19599c392c12" | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[1, 2, 3, 4]" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"[1, 2] + [3, 4]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "LL_TAQz03HRt", | |
"outputId": "9670a5a3-67ba-4b1d-fcc8-8f5c01ba8ed0" | |
}, | |
"outputs": [ | |
{ | |
"ename": "TypeError", | |
"evalue": "unsupported operand type(s) for +: 'dict' and 'dict'", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[1;32md:\\intro python\\poo-en-python.ipynb Cell 5'\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> <a href='vscode-notebook-cell:/d%3A/intro%20python/poo-en-python.ipynb#ch0000004?line=0'>1</a>\u001b[0m {\u001b[39m\"\u001b[39;49m\u001b[39mnombre\u001b[39;49m\u001b[39m\"\u001b[39;49m: \u001b[39m\"\u001b[39;49m\u001b[39mSergio\u001b[39;49m\u001b[39m\"\u001b[39;49m} \u001b[39m+\u001b[39;49m {\u001b[39m\"\u001b[39;49m\u001b[39mapellido\u001b[39;49m\u001b[39m\"\u001b[39;49m: \u001b[39m\"\u001b[39;49m\u001b[39mOrozco\u001b[39;49m\u001b[39m\"\u001b[39;49m}\n", | |
"\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'dict' and 'dict'" | |
] | |
} | |
], | |
"source": [ | |
"{\"nombre\": \"Sergio\"} + {\"apellido\": \"Orozco\"}" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "H3yImEj93HRt" | |
}, | |
"source": [ | |
"# Herencia y polimorfismo\n", | |
"\n", | |
"La herencia es un mecanismo de relación entre clases donde decimos que una clase es un subtipo de otra, por ejemplo como decir que \"Un gato es un Animal\"\n", | |
"\n", | |
"La herencia nos permite para heredar (tomar del padre): campos de datos, métodos de acceso de otra clase, ademas de poder añadir nuestros propios métodos y campos. Por lo tanto la herencia nos proporciona una manera de organizar el código para no tener que volver a repetir código." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 191 | |
}, | |
"id": "ZgZfE9Pf_MiS", | |
"outputId": "d00fc6ce-c130-45d2-a664-8ddff5012bfd" | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"image/svg+xml": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" contentStyleType=\"text/css\" height=\"186px\" preserveAspectRatio=\"none\" style=\"width:187px;height:186px;background:#FFFFFF;\" version=\"1.1\" viewBox=\"0 0 187 186\" width=\"187px\" zoomAndPan=\"magnify\"><defs/><g><!--MD5=[f1ee7a876834964f16097b5e53f21668]\nclass AnimalTerrestre--><g id=\"elem_AnimalTerrestre\"><rect fill=\"#F1F1F1\" height=\"64.2969\" id=\"AnimalTerrestre\" rx=\"2.5\" ry=\"2.5\" style=\"stroke:#181818;stroke-width:0.5;\" width=\"149\" x=\"18\" y=\"7\"/><ellipse cx=\"33\" cy=\"23\" fill=\"#ADD1B2\" rx=\"11\" ry=\"11\" style=\"stroke:#181818;stroke-width:1.0;\"/><path d=\"M35.3438,18.6719 C34.4063,18.2344 33.8125,18.0938 32.9375,18.0938 C30.3125,18.0938 28.3125,20.1719 28.3125,22.8906 L28.3125,24.0156 C28.3125,26.5938 30.4219,28.4844 33.3125,28.4844 C34.5313,28.4844 35.6875,28.1875 36.4375,27.6406 C37.0156,27.2344 37.3438,26.7813 37.3438,26.3906 C37.3438,25.9375 36.9531,25.5469 36.4844,25.5469 C36.2656,25.5469 36.0625,25.625 35.875,25.8125 C35.4219,26.2969 35.4219,26.2969 35.2344,26.3906 C34.8125,26.6563 34.125,26.7813 33.3594,26.7813 C31.3125,26.7813 30.0156,25.6875 30.0156,23.9844 L30.0156,22.8906 C30.0156,21.1094 31.2656,19.7969 33,19.7969 C33.5781,19.7969 34.1875,19.9531 34.6563,20.2031 C35.1406,20.4844 35.3125,20.7031 35.4063,21.1094 C35.4688,21.5156 35.5,21.6406 35.6406,21.7656 C35.7813,21.9063 36.0156,22.0156 36.2344,22.0156 C36.5,22.0156 36.7656,21.875 36.9375,21.6563 C37.0469,21.5 37.0781,21.3125 37.0781,20.8906 L37.0781,19.4688 C37.0781,19.0313 37.0625,18.9063 36.9688,18.75 C36.8125,18.4844 36.5313,18.3438 36.2344,18.3438 C35.9375,18.3438 35.7344,18.4375 35.5156,18.75 L35.3438,18.6719 Z \" fill=\"#000000\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"117\" x=\"47\" y=\"27.8467\">AnimalTerrestre</text><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"19\" x2=\"166\" y1=\"39\" y2=\"39\"/><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"19\" x2=\"166\" y1=\"47\" y2=\"47\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"67\" x=\"24\" y=\"63.9951\">caminar()</text></g><!--MD5=[911fe1357c24e8165700ac79875f9a94]\nclass Gato--><g id=\"elem_Gato\"><rect fill=\"#F1F1F1\" height=\"48\" id=\"Gato\" rx=\"2.5\" ry=\"2.5\" style=\"stroke:#181818;stroke-width:0.5;\" width=\"67\" x=\"7\" y=\"131\"/><ellipse cx=\"22\" cy=\"147\" fill=\"#ADD1B2\" rx=\"11\" ry=\"11\" style=\"stroke:#181818;stroke-width:1.0;\"/><path d=\"M24.3438,142.6719 C23.4063,142.2344 22.8125,142.0938 21.9375,142.0938 C19.3125,142.0938 17.3125,144.1719 17.3125,146.8906 L17.3125,148.0156 C17.3125,150.5938 19.4219,152.4844 22.3125,152.4844 C23.5313,152.4844 24.6875,152.1875 25.4375,151.6406 C26.0156,151.2344 26.3438,150.7813 26.3438,150.3906 C26.3438,149.9375 25.9531,149.5469 25.4844,149.5469 C25.2656,149.5469 25.0625,149.625 24.875,149.8125 C24.4219,150.2969 24.4219,150.2969 24.2344,150.3906 C23.8125,150.6563 23.125,150.7813 22.3594,150.7813 C20.3125,150.7813 19.0156,149.6875 19.0156,147.9844 L19.0156,146.8906 C19.0156,145.1094 20.2656,143.7969 22,143.7969 C22.5781,143.7969 23.1875,143.9531 23.6563,144.2031 C24.1406,144.4844 24.3125,144.7031 24.4063,145.1094 C24.4688,145.5156 24.5,145.6406 24.6406,145.7656 C24.7813,145.9063 25.0156,146.0156 25.2344,146.0156 C25.5,146.0156 25.7656,145.875 25.9375,145.6563 C26.0469,145.5 26.0781,145.3125 26.0781,144.8906 L26.0781,143.4688 C26.0781,143.0313 26.0625,142.9063 25.9688,142.75 C25.8125,142.4844 25.5313,142.3438 25.2344,142.3438 C24.9375,142.3438 24.7344,142.4375 24.5156,142.75 L24.3438,142.6719 Z \" fill=\"#000000\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"35\" x=\"36\" y=\"151.8467\">Gato</text><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"8\" x2=\"73\" y1=\"163\" y2=\"163\"/><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"8\" x2=\"73\" y1=\"171\" y2=\"171\"/></g><!--MD5=[838fb693084ebcda77f8479951cb9fdf]\nclass Perro--><g id=\"elem_Perro\"><rect fill=\"#F1F1F1\" height=\"48\" id=\"Perro\" rx=\"2.5\" ry=\"2.5\" style=\"stroke:#181818;stroke-width:0.5;\" width=\"71\" x=\"109\" y=\"131\"/><ellipse cx=\"124\" cy=\"147\" fill=\"#ADD1B2\" rx=\"11\" ry=\"11\" style=\"stroke:#181818;stroke-width:1.0;\"/><path d=\"M126.3438,142.6719 C125.4063,142.2344 124.8125,142.0938 123.9375,142.0938 C121.3125,142.0938 119.3125,144.1719 119.3125,146.8906 L119.3125,148.0156 C119.3125,150.5938 121.4219,152.4844 124.3125,152.4844 C125.5313,152.4844 126.6875,152.1875 127.4375,151.6406 C128.0156,151.2344 128.3438,150.7813 128.3438,150.3906 C128.3438,149.9375 127.9531,149.5469 127.4844,149.5469 C127.2656,149.5469 127.0625,149.625 126.875,149.8125 C126.4219,150.2969 126.4219,150.2969 126.2344,150.3906 C125.8125,150.6563 125.125,150.7813 124.3594,150.7813 C122.3125,150.7813 121.0156,149.6875 121.0156,147.9844 L121.0156,146.8906 C121.0156,145.1094 122.2656,143.7969 124,143.7969 C124.5781,143.7969 125.1875,143.9531 125.6563,144.2031 C126.1406,144.4844 126.3125,144.7031 126.4063,145.1094 C126.4688,145.5156 126.5,145.6406 126.6406,145.7656 C126.7813,145.9063 127.0156,146.0156 127.2344,146.0156 C127.5,146.0156 127.7656,145.875 127.9375,145.6563 C128.0469,145.5 128.0781,145.3125 128.0781,144.8906 L128.0781,143.4688 C128.0781,143.0313 128.0625,142.9063 127.9688,142.75 C127.8125,142.4844 127.5313,142.3438 127.2344,142.3438 C126.9375,142.3438 126.7344,142.4375 126.5156,142.75 L126.3438,142.6719 Z \" fill=\"#000000\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"39\" x=\"138\" y=\"151.8467\">Perro</text><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"110\" x2=\"179\" y1=\"163\" y2=\"163\"/><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"110\" x2=\"179\" y1=\"171\" y2=\"171\"/></g><!--MD5=[82fe76b909222c2daf50c62607410c17]\nreverse link AnimalTerrestre to Gato--><g id=\"link_AnimalTerrestre_Gato\"><path codeLine=\"1\" d=\"M69.9664,89.4007 C63.392,103.8138 56.5165,118.8869 51.0621,130.8446 \" fill=\"none\" id=\"AnimalTerrestre-backto-Gato\" style=\"stroke:#181818;stroke-width:1.0;\"/><polygon fill=\"none\" points=\"63.6219,86.4423,78.2908,71.151,76.3593,92.2524,63.6219,86.4423\" style=\"stroke:#181818;stroke-width:1.0;\"/></g><!--MD5=[8730a4bc0bb4699bb9f0fc41efc6c2e0]\nreverse link AnimalTerrestre to Perro--><g id=\"link_AnimalTerrestre_Perro\"><path codeLine=\"2\" d=\"M115.034,89.4007 C121.608,103.8138 128.483,118.8869 133.938,130.8446 \" fill=\"none\" id=\"AnimalTerrestre-backto-Perro\" style=\"stroke:#181818;stroke-width:1.0;\"/><polygon fill=\"none\" points=\"108.641,92.2524,106.7092,71.151,121.378,86.4423,108.641,92.2524\" style=\"stroke:#181818;stroke-width:1.0;\"/></g><!--MD5=[288992cf8ee0c6cad174cecb07101f9c]\n@startuml\nAnimalTerrestre <|- - Gato\nAnimalTerrestre <|- - Perro\n\nAnimalTerrestre : caminar()\n@enduml\n\nPlantUML version 1.2022.7beta2(Unknown compile time)\n(GPL source distribution)\nJava Runtime: Java(TM) SE Runtime Environment\nJVM: Java HotSpot(TM) 64-Bit Server VM\nDefault Encoding: UTF-8\nLanguage: en\nCountry: US\n--></g></svg>", | |
"text/plain": [ | |
"<IPython.core.display.SVG object>" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%%plantuml\n", | |
"\n", | |
"@startuml\n", | |
"AnimalTerrestre <|-- Gato\n", | |
"AnimalTerrestre <|-- Perro\n", | |
"\n", | |
"AnimalTerrestre : caminar()\n", | |
"@enduml" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "PjufjrgY3HRt", | |
"outputId": "f8b397aa-cf14-417d-b86b-b542928d2356" | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Animal Caminando\n", | |
"Gato Caminando\n" | |
] | |
} | |
], | |
"source": [ | |
"class AnimalTerrestre:\n", | |
" def caminar(self):\n", | |
" print(\"Animal Caminando\")\n", | |
"\n", | |
"class Perro(AnimalTerrestre):\n", | |
" pass\n", | |
"\n", | |
"class Gato(AnimalTerrestre):\n", | |
" def caminar(self):\n", | |
" print(\"Gato Caminando\")\n", | |
"\n", | |
" \n", | |
"perro = Perro()\n", | |
"perro.caminar()\n", | |
"gato = Gato()\n", | |
"gato.caminar()\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "C6N_j9pJDgnv" | |
}, | |
"source": [ | |
"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "qlRkoO7q3HRu" | |
}, | |
"source": [ | |
"# Herencia multiple\n", | |
"\n", | |
"Python soporta la herencia multiple, lo cual nos permite reutilizar código de todas las clases de las cuales se herede " | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 191 | |
}, | |
"id": "6hVYscxA_BbX", | |
"outputId": "a49db39c-f8ef-4079-eed9-563fa88a50d0" | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"image/svg+xml": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" contentStyleType=\"text/css\" height=\"170px\" preserveAspectRatio=\"none\" style=\"width:322px;height:170px;background:#FFFFFF;\" version=\"1.1\" viewBox=\"0 0 322 170\" width=\"322px\" zoomAndPan=\"magnify\"><defs/><g><!--MD5=[d4810177e0cd78618d5cf804f1eed890]\nclass MiSuperClase1--><g id=\"elem_MiSuperClase1\"><rect fill=\"#F1F1F1\" height=\"48\" id=\"MiSuperClase1\" rx=\"2.5\" ry=\"2.5\" style=\"stroke:#181818;stroke-width:0.5;\" width=\"137\" x=\"7\" y=\"7\"/><ellipse cx=\"22\" cy=\"23\" fill=\"#ADD1B2\" rx=\"11\" ry=\"11\" style=\"stroke:#181818;stroke-width:1.0;\"/><path d=\"M24.3438,18.6719 C23.4063,18.2344 22.8125,18.0938 21.9375,18.0938 C19.3125,18.0938 17.3125,20.1719 17.3125,22.8906 L17.3125,24.0156 C17.3125,26.5938 19.4219,28.4844 22.3125,28.4844 C23.5313,28.4844 24.6875,28.1875 25.4375,27.6406 C26.0156,27.2344 26.3438,26.7813 26.3438,26.3906 C26.3438,25.9375 25.9531,25.5469 25.4844,25.5469 C25.2656,25.5469 25.0625,25.625 24.875,25.8125 C24.4219,26.2969 24.4219,26.2969 24.2344,26.3906 C23.8125,26.6563 23.125,26.7813 22.3594,26.7813 C20.3125,26.7813 19.0156,25.6875 19.0156,23.9844 L19.0156,22.8906 C19.0156,21.1094 20.2656,19.7969 22,19.7969 C22.5781,19.7969 23.1875,19.9531 23.6563,20.2031 C24.1406,20.4844 24.3125,20.7031 24.4063,21.1094 C24.4688,21.5156 24.5,21.6406 24.6406,21.7656 C24.7813,21.9063 25.0156,22.0156 25.2344,22.0156 C25.5,22.0156 25.7656,21.875 25.9375,21.6563 C26.0469,21.5 26.0781,21.3125 26.0781,20.8906 L26.0781,19.4688 C26.0781,19.0313 26.0625,18.9063 25.9688,18.75 C25.8125,18.4844 25.5313,18.3438 25.2344,18.3438 C24.9375,18.3438 24.7344,18.4375 24.5156,18.75 L24.3438,18.6719 Z \" fill=\"#000000\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"105\" x=\"36\" y=\"27.8467\">MiSuperClase1</text><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"8\" x2=\"143\" y1=\"39\" y2=\"39\"/><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"8\" x2=\"143\" y1=\"47\" y2=\"47\"/></g><!--MD5=[ae7082bb1f0a94382099e0a7c594e576]\nclass MiClase--><g id=\"elem_MiClase\"><rect fill=\"#F1F1F1\" height=\"48\" id=\"MiClase\" rx=\"2.5\" ry=\"2.5\" style=\"stroke:#181818;stroke-width:0.5;\" width=\"86\" x=\"118.5\" y=\"115\"/><ellipse cx=\"133.5\" cy=\"131\" fill=\"#ADD1B2\" rx=\"11\" ry=\"11\" style=\"stroke:#181818;stroke-width:1.0;\"/><path d=\"M135.8438,126.6719 C134.9063,126.2344 134.3125,126.0938 133.4375,126.0938 C130.8125,126.0938 128.8125,128.1719 128.8125,130.8906 L128.8125,132.0156 C128.8125,134.5938 130.9219,136.4844 133.8125,136.4844 C135.0313,136.4844 136.1875,136.1875 136.9375,135.6406 C137.5156,135.2344 137.8438,134.7813 137.8438,134.3906 C137.8438,133.9375 137.4531,133.5469 136.9844,133.5469 C136.7656,133.5469 136.5625,133.625 136.375,133.8125 C135.9219,134.2969 135.9219,134.2969 135.7344,134.3906 C135.3125,134.6563 134.625,134.7813 133.8594,134.7813 C131.8125,134.7813 130.5156,133.6875 130.5156,131.9844 L130.5156,130.8906 C130.5156,129.1094 131.7656,127.7969 133.5,127.7969 C134.0781,127.7969 134.6875,127.9531 135.1563,128.2031 C135.6406,128.4844 135.8125,128.7031 135.9063,129.1094 C135.9688,129.5156 136,129.6406 136.1406,129.7656 C136.2813,129.9063 136.5156,130.0156 136.7344,130.0156 C137,130.0156 137.2656,129.875 137.4375,129.6563 C137.5469,129.5 137.5781,129.3125 137.5781,128.8906 L137.5781,127.4688 C137.5781,127.0313 137.5625,126.9063 137.4688,126.75 C137.3125,126.4844 137.0313,126.3438 136.7344,126.3438 C136.4375,126.3438 136.2344,126.4375 136.0156,126.75 L135.8438,126.6719 Z \" fill=\"#000000\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"54\" x=\"147.5\" y=\"135.8467\">MiClase</text><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"119.5\" x2=\"203.5\" y1=\"147\" y2=\"147\"/><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"119.5\" x2=\"203.5\" y1=\"155\" y2=\"155\"/></g><!--MD5=[f572b088d68f2e565ab338f074a0f1d5]\nclass MiSuperClase2--><g id=\"elem_MiSuperClase2\"><rect fill=\"#F1F1F1\" height=\"48\" id=\"MiSuperClase2\" rx=\"2.5\" ry=\"2.5\" style=\"stroke:#181818;stroke-width:0.5;\" width=\"136\" x=\"179.5\" y=\"7\"/><ellipse cx=\"194.5\" cy=\"23\" fill=\"#ADD1B2\" rx=\"11\" ry=\"11\" style=\"stroke:#181818;stroke-width:1.0;\"/><path d=\"M196.8438,18.6719 C195.9063,18.2344 195.3125,18.0938 194.4375,18.0938 C191.8125,18.0938 189.8125,20.1719 189.8125,22.8906 L189.8125,24.0156 C189.8125,26.5938 191.9219,28.4844 194.8125,28.4844 C196.0313,28.4844 197.1875,28.1875 197.9375,27.6406 C198.5156,27.2344 198.8438,26.7813 198.8438,26.3906 C198.8438,25.9375 198.4531,25.5469 197.9844,25.5469 C197.7656,25.5469 197.5625,25.625 197.375,25.8125 C196.9219,26.2969 196.9219,26.2969 196.7344,26.3906 C196.3125,26.6563 195.625,26.7813 194.8594,26.7813 C192.8125,26.7813 191.5156,25.6875 191.5156,23.9844 L191.5156,22.8906 C191.5156,21.1094 192.7656,19.7969 194.5,19.7969 C195.0781,19.7969 195.6875,19.9531 196.1563,20.2031 C196.6406,20.4844 196.8125,20.7031 196.9063,21.1094 C196.9688,21.5156 197,21.6406 197.1406,21.7656 C197.2813,21.9063 197.5156,22.0156 197.7344,22.0156 C198,22.0156 198.2656,21.875 198.4375,21.6563 C198.5469,21.5 198.5781,21.3125 198.5781,20.8906 L198.5781,19.4688 C198.5781,19.0313 198.5625,18.9063 198.4688,18.75 C198.3125,18.4844 198.0313,18.3438 197.7344,18.3438 C197.4375,18.3438 197.2344,18.4375 197.0156,18.75 L196.8438,18.6719 Z \" fill=\"#000000\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"104\" x=\"208.5\" y=\"27.8467\">MiSuperClase2</text><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"180.5\" x2=\"314.5\" y1=\"39\" y2=\"39\"/><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"180.5\" x2=\"314.5\" y1=\"47\" y2=\"47\"/></g><!--MD5=[0654f17fc7c81091c41c18896daade65]\nreverse link MiSuperClase1 to MiClase--><g id=\"link_MiSuperClase1_MiClase\"><path codeLine=\"1\" d=\"M106.8998,70.7021 C118.864,85.4485 132.123,101.7909 142.579,114.6784 \" fill=\"none\" id=\"MiSuperClase1-backto-MiClase\" style=\"stroke:#181818;stroke-width:1.0;\"/><polygon fill=\"none\" points=\"101.3256,74.942,94.1607,55,112.198,66.1214,101.3256,74.942\" style=\"stroke:#181818;stroke-width:1.0;\"/></g><!--MD5=[2041990ce8b5b900da124a09536a1333]\nreverse link MiSuperClase2 to MiClase--><g id=\"link_MiSuperClase2_MiClase\"><path codeLine=\"2\" d=\"M216.1,70.7021 C204.136,85.4485 190.877,101.7909 180.421,114.6784 \" fill=\"none\" id=\"MiSuperClase2-backto-MiClase\" style=\"stroke:#181818;stroke-width:1.0;\"/><polygon fill=\"none\" points=\"210.802,66.1214,228.839,55,221.674,74.942,210.802,66.1214\" style=\"stroke:#181818;stroke-width:1.0;\"/></g><!--MD5=[1abc2e5e2d071e577c917d4e08672f35]\n@startuml\nMiSuperClase1 <|- - MiClase\nMiSuperClase2 <|- - MiClase\n@enduml\n\nPlantUML version 1.2022.7beta2(Unknown compile time)\n(GPL source distribution)\nJava Runtime: Java(TM) SE Runtime Environment\nJVM: Java HotSpot(TM) 64-Bit Server VM\nDefault Encoding: UTF-8\nLanguage: en\nCountry: US\n--></g></svg>", | |
"text/plain": [ | |
"<IPython.core.display.SVG object>" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%%plantuml\n", | |
"\n", | |
"@startuml\n", | |
"MiSuperClase1 <|-- MiClase\n", | |
"MiSuperClase2 <|-- MiClase\n", | |
"@enduml" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "Gq7sFdAy3HRu" | |
}, | |
"outputs": [], | |
"source": [ | |
"class MiSuperClase1():\n", | |
" \n", | |
" def metodo_1(self):\n", | |
" print(\"metodo_1 metodo llamado\")\n", | |
" \n", | |
"class MiSuperClase2():\n", | |
" \n", | |
" def metodo_2(self):\n", | |
" print(\"metodo_2 metodo llamado\")\n", | |
"\n", | |
"# Clase que hereda las 2 clases anteriormente definidas\n", | |
"class MiClase(MiSuperClase1, MiSuperClase2):\n", | |
" \n", | |
" def metodo_3(self):\n", | |
" print(\"metodo_3 metodo llamado\")\n", | |
"\n", | |
"# Se crea el objeto \"c\" y luego se llama al metodo_1 y metodo_2 heredados de las 2 primeras clases\n", | |
"c = MiClase()\n", | |
"c.metodo_1()\n", | |
"c.metodo_2()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "Y52LK2VO3HRu" | |
}, | |
"source": [ | |
"# El problema del diamante\n", | |
"\n", | |
"Cuando tenemos herencia multiple puede suceder que heredemos de una clase la cual extiende de una base común, esto es lo que se llamaria el problema del diamante, y en python se resuelve escogiendo bien el orden en el cual heredamos los elementos" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 299 | |
}, | |
"id": "1qJpOTeJ8nnA", | |
"outputId": "96305041-54ad-46ce-82ad-a96e849f4fc3" | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"image/svg+xml": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" contentStyleType=\"text/css\" height=\"278px\" preserveAspectRatio=\"none\" style=\"width:133px;height:278px;background:#FFFFFF;\" version=\"1.1\" viewBox=\"0 0 133 278\" width=\"133px\" zoomAndPan=\"magnify\"><defs/><g><!--MD5=[a46ee5ce0ceb0380cf446a08a056dce4]\nclass A--><g id=\"elem_A\"><rect fill=\"#F1F1F1\" height=\"48\" id=\"A\" rx=\"2.5\" ry=\"2.5\" style=\"stroke:#181818;stroke-width:0.5;\" width=\"42\" x=\"45\" y=\"7\"/><ellipse cx=\"60\" cy=\"23\" fill=\"#ADD1B2\" rx=\"11\" ry=\"11\" style=\"stroke:#181818;stroke-width:1.0;\"/><path d=\"M62.3438,18.6719 C61.4063,18.2344 60.8125,18.0938 59.9375,18.0938 C57.3125,18.0938 55.3125,20.1719 55.3125,22.8906 L55.3125,24.0156 C55.3125,26.5938 57.4219,28.4844 60.3125,28.4844 C61.5313,28.4844 62.6875,28.1875 63.4375,27.6406 C64.0156,27.2344 64.3438,26.7813 64.3438,26.3906 C64.3438,25.9375 63.9531,25.5469 63.4844,25.5469 C63.2656,25.5469 63.0625,25.625 62.875,25.8125 C62.4219,26.2969 62.4219,26.2969 62.2344,26.3906 C61.8125,26.6563 61.125,26.7813 60.3594,26.7813 C58.3125,26.7813 57.0156,25.6875 57.0156,23.9844 L57.0156,22.8906 C57.0156,21.1094 58.2656,19.7969 60,19.7969 C60.5781,19.7969 61.1875,19.9531 61.6563,20.2031 C62.1406,20.4844 62.3125,20.7031 62.4063,21.1094 C62.4688,21.5156 62.5,21.6406 62.6406,21.7656 C62.7813,21.9063 63.0156,22.0156 63.2344,22.0156 C63.5,22.0156 63.7656,21.875 63.9375,21.6563 C64.0469,21.5 64.0781,21.3125 64.0781,20.8906 L64.0781,19.4688 C64.0781,19.0313 64.0625,18.9063 63.9688,18.75 C63.8125,18.4844 63.5313,18.3438 63.2344,18.3438 C62.9375,18.3438 62.7344,18.4375 62.5156,18.75 L62.3438,18.6719 Z \" fill=\"#000000\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"10\" x=\"74\" y=\"27.8467\">A</text><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"46\" x2=\"86\" y1=\"39\" y2=\"39\"/><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"46\" x2=\"86\" y1=\"47\" y2=\"47\"/></g><!--MD5=[69c451c751c414e37549b782190fef46]\nclass B--><g id=\"elem_B\"><rect fill=\"#F1F1F1\" height=\"48\" id=\"B\" rx=\"2.5\" ry=\"2.5\" style=\"stroke:#181818;stroke-width:0.5;\" width=\"42\" x=\"7\" y=\"115\"/><ellipse cx=\"22\" cy=\"131\" fill=\"#ADD1B2\" rx=\"11\" ry=\"11\" style=\"stroke:#181818;stroke-width:1.0;\"/><path d=\"M24.3438,126.6719 C23.4063,126.2344 22.8125,126.0938 21.9375,126.0938 C19.3125,126.0938 17.3125,128.1719 17.3125,130.8906 L17.3125,132.0156 C17.3125,134.5938 19.4219,136.4844 22.3125,136.4844 C23.5313,136.4844 24.6875,136.1875 25.4375,135.6406 C26.0156,135.2344 26.3438,134.7813 26.3438,134.3906 C26.3438,133.9375 25.9531,133.5469 25.4844,133.5469 C25.2656,133.5469 25.0625,133.625 24.875,133.8125 C24.4219,134.2969 24.4219,134.2969 24.2344,134.3906 C23.8125,134.6563 23.125,134.7813 22.3594,134.7813 C20.3125,134.7813 19.0156,133.6875 19.0156,131.9844 L19.0156,130.8906 C19.0156,129.1094 20.2656,127.7969 22,127.7969 C22.5781,127.7969 23.1875,127.9531 23.6563,128.2031 C24.1406,128.4844 24.3125,128.7031 24.4063,129.1094 C24.4688,129.5156 24.5,129.6406 24.6406,129.7656 C24.7813,129.9063 25.0156,130.0156 25.2344,130.0156 C25.5,130.0156 25.7656,129.875 25.9375,129.6563 C26.0469,129.5 26.0781,129.3125 26.0781,128.8906 L26.0781,127.4688 C26.0781,127.0313 26.0625,126.9063 25.9688,126.75 C25.8125,126.4844 25.5313,126.3438 25.2344,126.3438 C24.9375,126.3438 24.7344,126.4375 24.5156,126.75 L24.3438,126.6719 Z \" fill=\"#000000\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"10\" x=\"36\" y=\"135.8467\">B</text><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"8\" x2=\"48\" y1=\"147\" y2=\"147\"/><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"8\" x2=\"48\" y1=\"155\" y2=\"155\"/></g><!--MD5=[378b18254a1b2429f435e93f5e5cc50c]\nclass C--><g id=\"elem_C\"><rect fill=\"#F1F1F1\" height=\"48\" id=\"C\" rx=\"2.5\" ry=\"2.5\" style=\"stroke:#181818;stroke-width:0.5;\" width=\"42\" x=\"84\" y=\"115\"/><ellipse cx=\"99\" cy=\"131\" fill=\"#ADD1B2\" rx=\"11\" ry=\"11\" style=\"stroke:#181818;stroke-width:1.0;\"/><path d=\"M101.3438,126.6719 C100.4063,126.2344 99.8125,126.0938 98.9375,126.0938 C96.3125,126.0938 94.3125,128.1719 94.3125,130.8906 L94.3125,132.0156 C94.3125,134.5938 96.4219,136.4844 99.3125,136.4844 C100.5313,136.4844 101.6875,136.1875 102.4375,135.6406 C103.0156,135.2344 103.3438,134.7813 103.3438,134.3906 C103.3438,133.9375 102.9531,133.5469 102.4844,133.5469 C102.2656,133.5469 102.0625,133.625 101.875,133.8125 C101.4219,134.2969 101.4219,134.2969 101.2344,134.3906 C100.8125,134.6563 100.125,134.7813 99.3594,134.7813 C97.3125,134.7813 96.0156,133.6875 96.0156,131.9844 L96.0156,130.8906 C96.0156,129.1094 97.2656,127.7969 99,127.7969 C99.5781,127.7969 100.1875,127.9531 100.6563,128.2031 C101.1406,128.4844 101.3125,128.7031 101.4063,129.1094 C101.4688,129.5156 101.5,129.6406 101.6406,129.7656 C101.7813,129.9063 102.0156,130.0156 102.2344,130.0156 C102.5,130.0156 102.7656,129.875 102.9375,129.6563 C103.0469,129.5 103.0781,129.3125 103.0781,128.8906 L103.0781,127.4688 C103.0781,127.0313 103.0625,126.9063 102.9688,126.75 C102.8125,126.4844 102.5313,126.3438 102.2344,126.3438 C101.9375,126.3438 101.7344,126.4375 101.5156,126.75 L101.3438,126.6719 Z \" fill=\"#000000\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"10\" x=\"113\" y=\"135.8467\">C</text><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"85\" x2=\"125\" y1=\"147\" y2=\"147\"/><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"85\" x2=\"125\" y1=\"155\" y2=\"155\"/></g><!--MD5=[69b356fec5e4f53d7535011b6fc9d60b]\nclass D--><g id=\"elem_D\"><rect fill=\"#F1F1F1\" height=\"48\" id=\"D\" rx=\"2.5\" ry=\"2.5\" style=\"stroke:#181818;stroke-width:0.5;\" width=\"43\" x=\"44.5\" y=\"223\"/><ellipse cx=\"59.5\" cy=\"239\" fill=\"#ADD1B2\" rx=\"11\" ry=\"11\" style=\"stroke:#181818;stroke-width:1.0;\"/><path d=\"M61.8438,234.6719 C60.9063,234.2344 60.3125,234.0938 59.4375,234.0938 C56.8125,234.0938 54.8125,236.1719 54.8125,238.8906 L54.8125,240.0156 C54.8125,242.5938 56.9219,244.4844 59.8125,244.4844 C61.0313,244.4844 62.1875,244.1875 62.9375,243.6406 C63.5156,243.2344 63.8438,242.7813 63.8438,242.3906 C63.8438,241.9375 63.4531,241.5469 62.9844,241.5469 C62.7656,241.5469 62.5625,241.625 62.375,241.8125 C61.9219,242.2969 61.9219,242.2969 61.7344,242.3906 C61.3125,242.6563 60.625,242.7813 59.8594,242.7813 C57.8125,242.7813 56.5156,241.6875 56.5156,239.9844 L56.5156,238.8906 C56.5156,237.1094 57.7656,235.7969 59.5,235.7969 C60.0781,235.7969 60.6875,235.9531 61.1563,236.2031 C61.6406,236.4844 61.8125,236.7031 61.9063,237.1094 C61.9688,237.5156 62,237.6406 62.1406,237.7656 C62.2813,237.9063 62.5156,238.0156 62.7344,238.0156 C63,238.0156 63.2656,237.875 63.4375,237.6563 C63.5469,237.5 63.5781,237.3125 63.5781,236.8906 L63.5781,235.4688 C63.5781,235.0313 63.5625,234.9063 63.4688,234.75 C63.3125,234.4844 63.0313,234.3438 62.7344,234.3438 C62.4375,234.3438 62.2344,234.4375 62.0156,234.75 L61.8438,234.6719 Z \" fill=\"#000000\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"11\" x=\"73.5\" y=\"243.8467\">D</text><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"45.5\" x2=\"86.5\" y1=\"255\" y2=\"255\"/><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"45.5\" x2=\"86.5\" y1=\"263\" y2=\"263\"/></g><!--MD5=[81500ff05264bba268bb228b65a49ed8]\nreverse link A to B--><g id=\"link_A_B\"><path codeLine=\"1\" d=\"M50.923,74.057 C45.9643,87.889 40.6329,102.761 36.3606,114.678 \" fill=\"none\" id=\"A-backto-B\" style=\"stroke:#181818;stroke-width:1.0;\"/><polygon fill=\"none\" points=\"44.4159,71.465,57.7546,55,57.5947,76.189,44.4159,71.465\" style=\"stroke:#181818;stroke-width:1.0;\"/></g><!--MD5=[518367c1e7fbaa215fad71504a99bd4f]\nreverse link A to C--><g id=\"link_A_C\"><path codeLine=\"2\" d=\"M81.4738,74.057 C86.563,87.889 92.0347,102.761 96.4194,114.678 \" fill=\"none\" id=\"A-backto-C\" style=\"stroke:#181818;stroke-width:1.0;\"/><polygon fill=\"none\" points=\"74.7989,76.187,74.4624,55,87.9378,71.353,74.7989,76.187\" style=\"stroke:#181818;stroke-width:1.0;\"/></g><!--MD5=[aef4253f22c35495e0572246d48c1675]\nreverse link C to D--><g id=\"link_C_D\"><path codeLine=\"3\" d=\"M89.5262,182.0569 C84.437,195.8891 78.9653,210.7609 74.5806,222.6784 \" fill=\"none\" id=\"C-backto-D\" style=\"stroke:#181818;stroke-width:1.0;\"/><polygon fill=\"none\" points=\"83.0622,179.3532,96.5376,163,96.2011,184.1874,83.0622,179.3532\" style=\"stroke:#181818;stroke-width:1.0;\"/></g><!--MD5=[7ab6ecae7f500dbabd05e8a84c7483c7]\nreverse link B to D--><g id=\"link_B_D\"><path codeLine=\"4\" d=\"M43.077,182.0569 C48.0357,195.8891 53.3671,210.7609 57.6394,222.6784 \" fill=\"none\" id=\"B-backto-D\" style=\"stroke:#181818;stroke-width:1.0;\"/><polygon fill=\"none\" points=\"36.4053,184.1895,36.2454,163,49.5841,179.465,36.4053,184.1895\" style=\"stroke:#181818;stroke-width:1.0;\"/></g><!--MD5=[b8512ef239919e1880b3ad96d8c60cbb]\n@startuml\nA <|- - B\nA <|- - C\nC <|- - D\nB <|- - D\n@enduml\n\nPlantUML version 1.2022.7beta2(Unknown compile time)\n(GPL source distribution)\nJava Runtime: Java(TM) SE Runtime Environment\nJVM: Java HotSpot(TM) 64-Bit Server VM\nDefault Encoding: UTF-8\nLanguage: en\nCountry: US\n--></g></svg>", | |
"text/plain": [ | |
"<IPython.core.display.SVG object>" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%%plantuml\n", | |
"\n", | |
"@startuml\n", | |
"A <|-- B\n", | |
"A <|-- C\n", | |
"C <|-- D\n", | |
"B <|-- D\n", | |
"@enduml\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "Seltml5z3HRu", | |
"outputId": "89fb45aa-d9b0-44c8-b849-03be36e686fa" | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"I am the display of Class B\n" | |
] | |
} | |
], | |
"source": [ | |
"class A:\n", | |
" def display(self):\n", | |
" print(\"I am the display of Class A\")\n", | |
" \n", | |
" \n", | |
"class B(A):\n", | |
" def display(self):\n", | |
" print(\"I am the display of Class B\")\n", | |
" \n", | |
" \n", | |
"class C(A):\n", | |
" def display(self):\n", | |
" print(\"I am the display of Class C\")\n", | |
" \n", | |
" \n", | |
"class D(B, C):\n", | |
" pass\n", | |
" \n", | |
" \n", | |
"o = D()\n", | |
"o.display()\n", | |
" " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "0Bn2K3-V3HRu" | |
}, | |
"source": [ | |
"# Duck typing\n", | |
"\n", | |
"Algo muy interesante de python es la filosofia de duck typing, si camina como pato, parece un pato, entonces es un pato" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "I62FJiG6Dgnw", | |
"outputId": "eb75469c-bdf6-4aa4-c95a-73fcb66d5572" | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"image/svg+xml": "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" contentStyleType=\"text/css\" height=\"202px\" preserveAspectRatio=\"none\" style=\"width:202px;height:202px;background:#FFFFFF;\" version=\"1.1\" viewBox=\"0 0 202 202\" width=\"202px\" zoomAndPan=\"magnify\"><defs/><g><!--MD5=[90f66edba3ee2ef2943b92b287a7e5d6]\nclass Bird--><g id=\"elem_Bird\"><rect fill=\"#F1F1F1\" height=\"64.2969\" id=\"Bird\" rx=\"2.5\" ry=\"2.5\" style=\"stroke:#181818;stroke-width:0.5;\" width=\"61\" x=\"7\" y=\"7\"/><ellipse cx=\"22\" cy=\"23\" fill=\"#ADD1B2\" rx=\"11\" ry=\"11\" style=\"stroke:#181818;stroke-width:1.0;\"/><path d=\"M24.3438,18.6719 C23.4063,18.2344 22.8125,18.0938 21.9375,18.0938 C19.3125,18.0938 17.3125,20.1719 17.3125,22.8906 L17.3125,24.0156 C17.3125,26.5938 19.4219,28.4844 22.3125,28.4844 C23.5313,28.4844 24.6875,28.1875 25.4375,27.6406 C26.0156,27.2344 26.3438,26.7813 26.3438,26.3906 C26.3438,25.9375 25.9531,25.5469 25.4844,25.5469 C25.2656,25.5469 25.0625,25.625 24.875,25.8125 C24.4219,26.2969 24.4219,26.2969 24.2344,26.3906 C23.8125,26.6563 23.125,26.7813 22.3594,26.7813 C20.3125,26.7813 19.0156,25.6875 19.0156,23.9844 L19.0156,22.8906 C19.0156,21.1094 20.2656,19.7969 22,19.7969 C22.5781,19.7969 23.1875,19.9531 23.6563,20.2031 C24.1406,20.4844 24.3125,20.7031 24.4063,21.1094 C24.4688,21.5156 24.5,21.6406 24.6406,21.7656 C24.7813,21.9063 25.0156,22.0156 25.2344,22.0156 C25.5,22.0156 25.7656,21.875 25.9375,21.6563 C26.0469,21.5 26.0781,21.3125 26.0781,20.8906 L26.0781,19.4688 C26.0781,19.0313 26.0625,18.9063 25.9688,18.75 C25.8125,18.4844 25.5313,18.3438 25.2344,18.3438 C24.9375,18.3438 24.7344,18.4375 24.5156,18.75 L24.3438,18.6719 Z \" fill=\"#000000\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"29\" x=\"36\" y=\"27.8467\">Bird</text><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"8\" x2=\"67\" y1=\"39\" y2=\"39\"/><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"8\" x2=\"67\" y1=\"47\" y2=\"47\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"28\" x=\"13\" y=\"63.9951\">fly()</text></g><!--MD5=[c93bce01e5525b79bdbb6e4ef958107e]\nclass Airplane--><g id=\"elem_Airplane\"><rect fill=\"#F1F1F1\" height=\"64.2969\" id=\"Airplane\" rx=\"2.5\" ry=\"2.5\" style=\"stroke:#181818;stroke-width:0.5;\" width=\"92\" x=\"103.5\" y=\"7\"/><ellipse cx=\"118.5\" cy=\"23\" fill=\"#ADD1B2\" rx=\"11\" ry=\"11\" style=\"stroke:#181818;stroke-width:1.0;\"/><path d=\"M120.8438,18.6719 C119.9063,18.2344 119.3125,18.0938 118.4375,18.0938 C115.8125,18.0938 113.8125,20.1719 113.8125,22.8906 L113.8125,24.0156 C113.8125,26.5938 115.9219,28.4844 118.8125,28.4844 C120.0313,28.4844 121.1875,28.1875 121.9375,27.6406 C122.5156,27.2344 122.8438,26.7813 122.8438,26.3906 C122.8438,25.9375 122.4531,25.5469 121.9844,25.5469 C121.7656,25.5469 121.5625,25.625 121.375,25.8125 C120.9219,26.2969 120.9219,26.2969 120.7344,26.3906 C120.3125,26.6563 119.625,26.7813 118.8594,26.7813 C116.8125,26.7813 115.5156,25.6875 115.5156,23.9844 L115.5156,22.8906 C115.5156,21.1094 116.7656,19.7969 118.5,19.7969 C119.0781,19.7969 119.6875,19.9531 120.1563,20.2031 C120.6406,20.4844 120.8125,20.7031 120.9063,21.1094 C120.9688,21.5156 121,21.6406 121.1406,21.7656 C121.2813,21.9063 121.5156,22.0156 121.7344,22.0156 C122,22.0156 122.2656,21.875 122.4375,21.6563 C122.5469,21.5 122.5781,21.3125 122.5781,20.8906 L122.5781,19.4688 C122.5781,19.0313 122.5625,18.9063 122.4688,18.75 C122.3125,18.4844 122.0313,18.3438 121.7344,18.3438 C121.4375,18.3438 121.2344,18.4375 121.0156,18.75 L120.8438,18.6719 Z \" fill=\"#000000\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"60\" x=\"132.5\" y=\"27.8467\">Airplane</text><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"104.5\" x2=\"194.5\" y1=\"39\" y2=\"39\"/><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"104.5\" x2=\"194.5\" y1=\"47\" y2=\"47\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"28\" x=\"109.5\" y=\"63.9951\">fly()</text></g><!--MD5=[0ba60c1c7eece7977c024cb94d0d3fa3]\nclass Fish--><g id=\"elem_Fish\"><rect fill=\"#F1F1F1\" height=\"64.2969\" id=\"Fish\" rx=\"2.5\" ry=\"2.5\" style=\"stroke:#181818;stroke-width:0.5;\" width=\"60\" x=\"7.5\" y=\"131\"/><ellipse cx=\"22.5\" cy=\"147\" fill=\"#ADD1B2\" rx=\"11\" ry=\"11\" style=\"stroke:#181818;stroke-width:1.0;\"/><path d=\"M24.8438,142.6719 C23.9063,142.2344 23.3125,142.0938 22.4375,142.0938 C19.8125,142.0938 17.8125,144.1719 17.8125,146.8906 L17.8125,148.0156 C17.8125,150.5938 19.9219,152.4844 22.8125,152.4844 C24.0313,152.4844 25.1875,152.1875 25.9375,151.6406 C26.5156,151.2344 26.8438,150.7813 26.8438,150.3906 C26.8438,149.9375 26.4531,149.5469 25.9844,149.5469 C25.7656,149.5469 25.5625,149.625 25.375,149.8125 C24.9219,150.2969 24.9219,150.2969 24.7344,150.3906 C24.3125,150.6563 23.625,150.7813 22.8594,150.7813 C20.8125,150.7813 19.5156,149.6875 19.5156,147.9844 L19.5156,146.8906 C19.5156,145.1094 20.7656,143.7969 22.5,143.7969 C23.0781,143.7969 23.6875,143.9531 24.1563,144.2031 C24.6406,144.4844 24.8125,144.7031 24.9063,145.1094 C24.9688,145.5156 25,145.6406 25.1406,145.7656 C25.2813,145.9063 25.5156,146.0156 25.7344,146.0156 C26,146.0156 26.2656,145.875 26.4375,145.6563 C26.5469,145.5 26.5781,145.3125 26.5781,144.8906 L26.5781,143.4688 C26.5781,143.0313 26.5625,142.9063 26.4688,142.75 C26.3125,142.4844 26.0313,142.3438 25.7344,142.3438 C25.4375,142.3438 25.2344,142.4375 25.0156,142.75 L24.8438,142.6719 Z \" fill=\"#000000\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"28\" x=\"36.5\" y=\"151.8467\">Fish</text><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"8.5\" x2=\"66.5\" y1=\"163\" y2=\"163\"/><line style=\"stroke:#181818;stroke-width:0.5;\" x1=\"8.5\" x2=\"66.5\" y1=\"171\" y2=\"171\"/><text fill=\"#000000\" font-family=\"sans-serif\" font-size=\"14\" lengthAdjust=\"spacing\" textLength=\"46\" x=\"13.5\" y=\"187.9951\">swim()</text></g><!--MD5=[761036587c7967366c367a2563f1ed62]\n@startuml\nBird : fly()\nAirplane : fly()\nFish : swim()\n@enduml\n\nPlantUML version 1.2022.7beta2(Unknown compile time)\n(GPL source distribution)\nJava Runtime: Java(TM) SE Runtime Environment\nJVM: Java HotSpot(TM) 64-Bit Server VM\nDefault Encoding: UTF-8\nLanguage: en\nCountry: US\n--></g></svg>", | |
"text/plain": [ | |
"<IPython.core.display.SVG object>" | |
] | |
}, | |
"execution_count": 32, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%%plantuml\n", | |
"\n", | |
"@startuml\n", | |
"Bird : fly()\n", | |
"Airplane : fly()\n", | |
"Fish : swim()\n", | |
"@enduml" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "ix30-m4h3HRu", | |
"outputId": "07d9b6ed-639f-40cd-8259-98bd1bbcf05b" | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"fly with wings\n", | |
"fly with fuel\n" | |
] | |
}, | |
{ | |
"ename": "AttributeError", | |
"evalue": "'Fish' object has no attribute 'fly'", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[1;32md:\\intro python\\poo_en_python_with_plant.ipynb Cell 39'\u001b[0m in \u001b[0;36m<cell line: 13>\u001b[1;34m()\u001b[0m\n\u001b[0;32m <a href='vscode-notebook-cell:/d%3A/intro%20python/poo_en_python_with_plant.ipynb#ch0000034?line=10'>11</a>\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mfish swim in sea\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m <a href='vscode-notebook-cell:/d%3A/intro%20python/poo_en_python_with_plant.ipynb#ch0000034?line=12'>13</a>\u001b[0m \u001b[39mfor\u001b[39;00m obj \u001b[39min\u001b[39;00m Bird(), Airplane(), Fish():\n\u001b[1;32m---> <a href='vscode-notebook-cell:/d%3A/intro%20python/poo_en_python_with_plant.ipynb#ch0000034?line=13'>14</a>\u001b[0m obj\u001b[39m.\u001b[39;49mfly()\n", | |
"\u001b[1;31mAttributeError\u001b[0m: 'Fish' object has no attribute 'fly'" | |
] | |
} | |
], | |
"source": [ | |
"class Bird:\n", | |
" def fly(self):\n", | |
" print(\"fly with wings\")\n", | |
" \n", | |
"class Airplane:\n", | |
" def fly(self):\n", | |
" print(\"fly with fuel\")\n", | |
" \n", | |
"class Fish:\n", | |
" def swim(self):\n", | |
" print(\"fish swim in sea\")\n", | |
"\n", | |
"for obj in Bird(), Airplane(), Fish():\n", | |
" obj.fly()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "iZxnwV0q3HRv" | |
}, | |
"source": [ | |
"## Como importamos dependencias externas\n", | |
"\n", | |
"No se pierdan la sesión 3 y la ultima de esta serie de tutoriales\n", | |
"\n", | |
"Vamos a hablar de como creamos un proyecto trabajando con ficheros individuales mostrando como consumir un API en python usando requests" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "kpxu7DXn3HRv" | |
}, | |
"source": [ | |
"# Conclusión\n", | |
"\n", | |
"Python es un lenguaje bastante potente en el cual puedes realizar entre otras cosas automatizaciones de tareas repetitivas, o bien practicar tu lógica en programación, de hecho seria interesante si en este ultimo bloque escribes una funcion que te diga si una palabra es palidroma o no, (se lee igual si escribes los caracteres al reves), y un conversor de temperaturas" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "ag18cElp3HRv" | |
}, | |
"source": [ | |
"DataCamp es una plataforma que sirve para aprender a programar (está en ingles)echa\n", | |
"https://www.datacamp.com/courses/intro-to-python-for-data-science\n", | |
"\n", | |
"Refactoring guru es un sitio donde se pueden encontrar ejemplos de como implementar patrones de código en distintos lenguajes de programación\n", | |
"https://refactoring.guru/es/design-patterns/python\n", | |
"\n", | |
"Curso de python para principiantes de microsoft\n", | |
"https://docs.microsoft.com/es-es/shows/intro-to-python-development/\n", | |
"\n", | |
"Magic methods in python\n", | |
"https://www.netjstech.com/2019/06/operator-overloading-in-python.html\n", | |
"\n", | |
"Clases en python\n", | |
"https://pythondiario.com/2016/10/herencia-y-polimorfismo-en-python.html#:~:text=La%20herencia%20en%20Python%20nos%20permite%20a%20los,permite%20escribir%20un%20c%C3%B3digo%20m%C3%A1s%20limpio%20y%20legible.\n", | |
"\n", | |
"Sobrecargada de operadores\n", | |
"https://pharos.sh/sobrecargar-funciones-y-operadores-en-python/#:~:text=Sobrecarga%20del%20operador%20Python%20nos%20permite%20cambiar%20el,operadores%20de%20Python%20depende%20de%20las%20clases%20integradas.\n" | |
] | |
} | |
], | |
"metadata": { | |
"colab": { | |
"collapsed_sections": [], | |
"name": "poo_en_python_with_plant.ipynb", | |
"provenance": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"display_name": "Python 3.10.5 64-bit (windows store)", | |
"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.10.5" | |
}, | |
"orig_nbformat": 4, | |
"vscode": { | |
"interpreter": { | |
"hash": "9eea658eba0ace8b4a7abaa509b0b66160d954cacf599d4eb5148fe00039b3be" | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment