{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "zTR24qd8n7St" }, "source": [ "# Ejemplos sin herencia [5]" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "iwMBeJH4n7Sw" }, "outputs": [], "source": [ "class MyFirstClass:\n", " pass" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "lQKi1Ffyn7Sx", "outputId": "0ae57c94-5b85-41b6-f033-adcf4e11c7c1", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "<__main__.MyFirstClass object at 0x7a4548d5ead0>\n", "<__main__.MyFirstClass object at 0x7a453be4be80>\n" ] } ], "source": [ "class MyFirstClass:\n", " pass\n", "\n", "a = MyFirstClass()\n", "b = MyFirstClass()\n", "print(a)\n", "print(b)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "id": "AddEU-aGn7Sz", "outputId": "f9f33e95-7eb6-42c5-9543-9c6e0cdc0400", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "5 4\n", "3 6\n" ] } ], "source": [ "class Point:\n", " pass\n", "\n", "p1 = Point()\n", "p2 = Point()\n", "\n", "p1.x = 5\n", "p1.y = 4\n", "\n", "p2.x = 3\n", "p2.y = 6\n", "\n", "print(p1.x, p1.y)\n", "print(p2.x, p2.y)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "id": "NOi-RgU2n7S0", "outputId": "05d53bb4-d7d5-407d-882a-730a105ff5f2", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "0 0\n" ] } ], "source": [ "class Point:\n", " def reset(self):\n", " self.x = 0\n", " self.y = 0\n", "\n", "p = Point()\n", "p.reset()\n", "print(p.x, p.y)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "id": "Isq3Tzoyn7S2", "outputId": "5266fe19-068f-45ca-8e3b-d68e53e3d51b", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "5.0\n", "4.47213595499958\n", "0.0\n" ] } ], "source": [ "import math\n", "\n", "class Point:\n", " def move(self, x, y):\n", " self.x = x\n", " self.y = y\n", " def reset(self):\n", " self.move(0, 0)\n", " def calculate_distance(self, other_point):\n", " return math.sqrt(\n", " (self.x - other_point.x)**2 +\n", " (self.y - other_point.y)**2)\n", "\n", "\n", "# how to use it:\n", "point1 = Point()\n", "point2 = Point()\n", "\n", "point1.reset()\n", "point2.move(5, 0)\n", "print(point2.calculate_distance(point1)) # Debugging and Testing the code\n", "assert (point2.calculate_distance(point1) ==\n", " point1.calculate_distance(point2)) # assert permite realizar comprobaciones\n", "point1.move(3, 4)\n", "print(point1.calculate_distance(point2))\n", "print(point1.calculate_distance(point1))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "id": "rQtXUcE_n7S4", "outputId": "b64c57e5-198d-4b23-e19a-e3993e3bd567", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "3 5\n" ] } ], "source": [ "class Point:\n", " def __init__(self, x, y):\n", " self.move(x, y)\n", "\n", " def move(self, x, y):\n", " self.x = x\n", " self.y = y\n", "\n", " def reset(self):\n", " self.move(0, 0)\n", "\n", "# Constructing a Point\n", "point = Point(3, 5)\n", "print(point.x, point.y)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "id": "-1ZZgYhvn7S6" }, "outputs": [], "source": [ "class Point:\n", " def __init__(self, x=0, y=0):\n", " self.move(x, y)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "id": "uvbXMwlun7S7" }, "outputs": [], "source": [ "import math\n", "\n", "class Point:\n", " 'Represents a point in two-dimensional geometric coordinates'\n", "\n", " def __init__(self, x=0, y=0):\n", " '''Initialize the position of a new point. The x and y coordinates can\n", " be specified. If they are not, the point defaults to the origin.'''\n", " self.move(x, y)\n", "\n", " def move(self, x, y):\n", " \"Move the point to a new location in two-dimensional space.\"\n", " self.x = x\n", " self.y = y\n", "\n", " def reset(self):\n", " 'Reset the point back to the geometric origin: 0, 0'\n", " self.move(0, 0)\n", "\n", " def calculate_distance(self, other_point):\n", " \"\"\"Calculate the distance from this point to a second point passed\n", " as a parameter.\n", " This function uses the Pythagorean Theorem to calculate the distance\n", " between the two points. The distance is returned as a float.\"\"\"\n", "\n", " return math.sqrt(\n", " (self.x - other_point.x)**2 +\n", " (self.y - other_point.y)**2)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "id": "cV6hAEaWn7S8" }, "outputs": [], "source": [ "class Database:\n", " # the database implementation\n", " pass\n", "\n", "database = Database()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "id": "7rN-J8Urn7S9" }, "outputs": [], "source": [ "class Database:\n", " # the database implementation\n", " pass\n", "\n", "database = None\n", "\n", "def initialize_database():\n", " global database\n", " database = Database()" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "id": "QBetPx0an7S-", "outputId": "5aaae656-f312-4fba-a087-9d95f5ffff4c", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "<__main__.UsefulClass object at 0x7a453be921a0>\n" ] } ], "source": [ "class UsefulClass:\n", " '''This class might be useful to other modules.'''\n", " pass\n", "\n", "def main():\n", " '''creates a useful class and does something with it for our module.'''\n", " useful = UsefulClass()\n", " print(useful)\n", "\n", "if __name__ == \"__main__\":\n", " main()" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "id": "rbwu3HQbn7S_", "outputId": "2e2a0595-9a23-4b66-827c-554f38de0641", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ " input: hello world, how are you today?\n", "output: Hello World, How Are You Today?\n" ] } ], "source": [ "def format_string(string, formatter=None):\n", " '''Format a string using the formatter object, which\n", " is expected to have a format() method that accepts\n", " a string.'''\n", " class DefaultFormatter:\n", " '''Format a string in title case.'''\n", " def format(self, string):\n", " return str(string).title()\n", "\n", " if not formatter:\n", " formatter = DefaultFormatter()\n", "\n", " return formatter.format(string)\n", "\n", "hello_string = \"hello world, how are you today?\"\n", "print(\" input: \" + hello_string)\n", "print(\"output: \" + format_string(hello_string))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "id": "OamtSUdhn7TA", "outputId": "9cd74b15-df79-4a54-c47a-94db5ce52103", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Los pilares de la tierra'" ], "application/vnd.google.colaboratory.intrinsic+json": { "type": "string" } }, "metadata": {}, "execution_count": 13 } ], "source": [ "class SecretString:\n", " '''A not-at-all secure way to store a secret string.'''\n", "\n", " def __init__(self, plain_string, pass_phrase):\n", " self.__plain_string = plain_string\n", " self.__pass_phrase = pass_phrase\n", "\n", " def decrypt(self, pass_phrase):\n", " '''Only show the string if the pass_phrase is correct.'''\n", " if pass_phrase == self.__pass_phrase:\n", " return self.__plain_string\n", " else:\n", " return ''\n", "\n", "s = SecretString(\"Los pilares de la tierra\", \"abcde\")\n", "s.decrypt(\"abcde\")" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "id": "hBvge6zKn7TB", "outputId": "10db559e-5545-41ae-c4ae-6ae001cb4a10", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 14 } ], "source": [ "import datetime\n", "\n", "last_id = 0\n", "\n", "class Note:\n", " '''Represent a note in the notebook. Match against a\n", " string in searches and store tags for each note.'''\n", "\n", "\n", " def __init__(self, memo, tags=''):\n", " '''initialize a note with memo and optional\n", " space-separated tags. Automatically set the note's\n", " creation date and a unique id'''\n", " self.memo = memo\n", " self.tags = tags\n", " self.creation_date = datetime.date.today()\n", " global last_id\n", " last_id += 1\n", " self.id = last_id\n", "\n", " def match(self, filter):\n", " '''Determine if this note matches the filter\n", " text. Return True if it matches, False otherwise.\n", "\n", " Search is case sensitive and matches both text and\n", " tags.'''\n", " return filter in self.memo or filter in self.tags\n", "\n", "n1 = Note(\"ProgramaciĆ³n orientada a objetos en Python\", \"POO,Python,programaciĆ³n\")\n", "n1.match(\"Python\")" ] } ], "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_0200.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 0 }