{ "cells": [ { "cell_type": "markdown", "id": "1a073c57-8dd4-4d17-9762-95aadf9955dd", "metadata": {}, "source": [ "Encapsulation, inheritance, and polymorphism are concepts of Object Oriented Programming." ] }, { "cell_type": "markdown", "id": "38f19049-177d-470f-99db-6c14025381ec", "metadata": {}, "source": [ "# 1. Builtin Functions on Attributes" ] }, { "cell_type": "markdown", "id": "905e0929-9193-43b8-88bb-129bc8f65687", "metadata": {}, "source": [ "We can get direct access to any object via the dot followed by the name of the attribute. We can also check whether an attribute is defined." ] }, { "cell_type": "code", "execution_count": 1, "id": "4d3a0d30-bd87-4e4f-91b4-9ece4d327e95", "metadata": {}, "outputs": [], "source": [ "import classpoint" ] }, { "cell_type": "code", "execution_count": 2, "id": "97977ba5-7745-43c3-81d9-f9d9437190dd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on module classpoint:\n", "\n", "NAME\n", " classpoint - Base class to start illustrating inheritance.\n", "\n", "CLASSES\n", " builtins.object\n", " Point\n", " \n", " class Point(builtins.object)\n", " | Point(a=0, b=0)\n", " | \n", " | defines a point in the plane\n", " | \n", " | Methods defined here:\n", " | \n", " | __init__(self, a=0, b=0)\n", " | constructs a point in the plane\n", " | \n", " | __str__(self)\n", " | returns the string representation of a point\n", " | \n", " | ----------------------------------------------------------------------\n", " | Data descriptors defined here:\n", " | \n", " | __dict__\n", " | dictionary for instance variables (if defined)\n", " | \n", " | __weakref__\n", " | list of weak references to the object (if defined)\n", "\n", "FILE\n", " c:\\users\\jan\\courses\\mcs260\\summer23\\lec28\\classpoint.py\n", "\n", "\n" ] } ], "source": [ "help(classpoint)" ] }, { "cell_type": "code", "execution_count": 3, "id": "f93f4d84-74e8-40a7-aca9-10e20a473fc3", "metadata": {}, "outputs": [], "source": [ "from classpoint import Point" ] }, { "cell_type": "code", "execution_count": 4, "id": "53d7c7c8-0823-4421-afb0-4bfef05b3aa6", "metadata": {}, "outputs": [], "source": [ "p = Point(2, 3)" ] }, { "cell_type": "code", "execution_count": 5, "id": "6d04bfe4-44d7-4951-91ee-1461f8f224ec", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hasattr(p, 'xpt')" ] }, { "cell_type": "code", "execution_count": 6, "id": "bb54676f-1ab2-4f63-90ca-bf2250db87ad", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "getattr(p, 'xpt')" ] }, { "cell_type": "code", "execution_count": 7, "id": "4177af08-3974-49a3-b3f4-743392cbc408", "metadata": {}, "outputs": [], "source": [ "setattr(p, 'xpt', 10)" ] }, { "cell_type": "code", "execution_count": 8, "id": "8efef827-0018-468c-b07c-648bad61ebca", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'( 1.0000e+01, 3.0000e+00 )'" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(p)" ] }, { "cell_type": "markdown", "id": "3adb394e-2307-478b-a3cf-934658d301b0", "metadata": {}, "source": [ "The examples above demonstrated the manipulation of the x-coordinate of the point via the name of the attribute, passed as the string `'xpt'` in the `hasattr()`, `getattr()`, and `setattr()`." ] }, { "cell_type": "code", "execution_count": 9, "id": "9a8ac0c6-c473-4d94-a4fc-cfe3eb51067a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p.xpt" ] }, { "cell_type": "markdown", "id": "093a0bb6-8ae5-4383-894c-51dd97be625d", "metadata": {}, "source": [ "# 2. Verifying Inheritance" ] }, { "cell_type": "markdown", "id": "20549e45-d04e-4272-92ab-6a44c4c15c30", "metadata": {}, "source": [ "The class `Circle` inherits from the class `Point`. We can verify this relationship with `issubclass()`." ] }, { "cell_type": "code", "execution_count": 10, "id": "57528a6b-0077-4a8b-8a62-4cd454c3afd4", "metadata": {}, "outputs": [], "source": [ "from classcircle import Circle" ] }, { "cell_type": "code", "execution_count": 11, "id": "82349a16-21e2-431b-8789-9a8df8b2aa2e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "issubclass(Point, Circle)" ] }, { "cell_type": "code", "execution_count": 12, "id": "b3a5ec88-f149-41c6-bdb4-96ab87ee8f7c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "issubclass(Circle, Point)" ] }, { "cell_type": "markdown", "id": "cc28c411-b48c-4ec2-a8de-11757244eb47", "metadata": {}, "source": [ "In the code of `Circle`, to make the circle inherit from the point:\n", "\n", "1. define the class via `class Circle(Point):`\n", "\n", "2. in the constructor of the class `Circle`: call the constructor of `Point`." ] }, { "cell_type": "code", "execution_count": 13, "id": "908d8c60-8e29-411a-a402-3889d18f88ee", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function __init__ in module classcircle:\n", "\n", "__init__(self, a=0, b=0, r=0)\n", " the center is (a,b), radius = r\n", "\n" ] } ], "source": [ "help(Circle.__init__)" ] }, { "cell_type": "code", "execution_count": 14, "id": "272fa241-0f89-4a82-b96d-12c1be812160", "metadata": {}, "outputs": [], "source": [ "c = Circle(1, 2, 3)" ] }, { "cell_type": "code", "execution_count": 15, "id": "fc89c33c-9399-4c12-adf5-96fa3951e63c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "center : ( 1.0000e+00, 2.0000e+00 )\n", "radius : 3.0000e+00\n" ] } ], "source": [ "print(c)" ] }, { "cell_type": "code", "execution_count": 16, "id": "d6e2917e-dcc9-4109-a48e-00707e881aa3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "28.274333882308138" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.area()" ] }, { "cell_type": "markdown", "id": "1726862c-b305-46ce-91dc-ca153176ac54", "metadata": {}, "source": [ "Observe the difference between `str(c)` and `c.area()`. Both are methods, but we cannot do `area(c)`." ] }, { "cell_type": "markdown", "id": "12f11fba-3b3d-430f-817e-b3c4c3c01321", "metadata": {}, "source": [ "# 3. Polynomials in One Variable" ] }, { "cell_type": "code", "execution_count": 17, "id": "2f10179a-e259-4939-a4f3-0b830976d276", "metadata": {}, "outputs": [], "source": [ "from classpoly import Poly" ] }, { "cell_type": "code", "execution_count": 18, "id": "97685422-3d33-4094-8ce6-c000d724fcef", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function __init__ in module classpoly:\n", "\n", "__init__(self, c=0, p=0)\n", " Defines a monomial with coefficient c and power p.\n", "\n" ] } ], "source": [ "help(Poly.__init__)" ] }, { "cell_type": "code", "execution_count": 19, "id": "dd79200a-0e0c-4a0e-af5b-0c2787a6b3f3", "metadata": {}, "outputs": [], "source": [ "q = Poly(2, 4) + Poly(1, 0)" ] }, { "cell_type": "code", "execution_count": 20, "id": "778e83cd-a1aa-4c4a-8694-6a7cfacd5c8c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "+2*x^4+1*x^0\n" ] } ], "source": [ "print(q)" ] }, { "cell_type": "code", "execution_count": 21, "id": "eb04fb18-09ca-4e28-8ccc-ab0ed639a04e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "163" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "q(3)" ] }, { "cell_type": "markdown", "id": "cf93b396-54be-422d-b21b-3e8a98a53e33", "metadata": {}, "source": [ "The last statement shows that `q` is a *callable object*." ] }, { "cell_type": "markdown", "id": "ca5c9a23-55c4-4d3f-bde9-fbff5051c5e1", "metadata": {}, "source": [ "Storing the terms of the polynomial as `__terms` makes that we cannot access the data of `q` via the attributes." ] }, { "cell_type": "code", "execution_count": 22, "id": "9f264f92-f5d8-4262-9491-e6765ae61a96", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hasattr(q, 'terms')" ] }, { "cell_type": "code", "execution_count": 23, "id": "cbf5947b-0164-4e35-a487-f063899236bf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hasattr(q, '__terms')" ] }, { "cell_type": "markdown", "id": "43ae44b7-ec6f-4e4c-80a9-22a799d43af8", "metadata": {}, "source": [ "# 4. Wrapping Turtle" ] }, { "cell_type": "markdown", "id": "169e8bbf-4ab3-4222-8ef0-bad07a34efa8", "metadata": {}, "source": [ "The moon orbiting the earth, the earth orbiting the sun is one illustration of wrapping turtle objects, in making plots." ] }, { "cell_type": "markdown", "id": "491f82ca-8239-4510-a87f-73675692f8f3", "metadata": {}, "source": [ "Another illustration is a robot drawing a spiral." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10" } }, "nbformat": 4, "nbformat_minor": 5 }