{ "cells": [ { "cell_type": "markdown", "id": "3353846b-7487-4833-8ed0-f3c1de1325c3", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2023
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "id": "149aa052-0d06-441d-9f5f-6a2153b82cb7", "metadata": {}, "source": [ "# Special methods\n", "#### A class has more than just the special methods `__repr__()` and `__str__()` that we can override. For example, there are special methods that Python will implicitly call it executes arithmetic expressions with the operators `+`, `-`, `*`, `/`, `//`, and `%`:\n", "| Expression | Special method |\n", "| :-: | --- |\n", "| `x + y` | `__add__(self, y)` |\n", "| `x - y` | `__sub__(self, y)` |\n", "| `x * y` | `__mul__(self, y)` |\n", "| `x / y` | `__truediv__(self, y)` |\n", "| `x // y` | `__floordiv__(self, y)` |\n", "| `x % y` | `__mod__(self, y)` |\n", "\n", "#### For a more complete list of special methods, see [Special Methods](https://www.informit.com/articles/article.aspx?p=453682&seqNum=6)." ] }, { "cell_type": "markdown", "id": "406725f5-ade2-42ac-aa32-687abc3528ab", "metadata": {}, "source": [ "## A `Fraction` class\n", "#### We define a fracton object by specifying its numerator and denominator, such as `Fraction(3, 4)` for the fraction 3/4. The constructor should also reduce the fraction if it can, so `Fraction(8, 12)` is the fraction 2/3." ] }, { "cell_type": "code", "execution_count": null, "id": "98f0d3f8-29ed-4d3b-afaa-8b9748b895c5", "metadata": {}, "outputs": [], "source": [ "from fraction import Fraction\n", "\n", "print(f\"{Fraction(3, 4) = }\")\n", "print(f\"{Fraction(8, 12) = }\")" ] }, { "cell_type": "markdown", "id": "8a51db2e-35b1-4a49-82d1-30c61729c6cc", "metadata": {}, "source": [ "#### The `Fraction` class should define an `add()` method." ] }, { "cell_type": "code", "execution_count": null, "id": "94ba8821-b10b-42ec-9f15-be495217e39a", "metadata": {}, "outputs": [], "source": [ "f1 = Fraction(3, 4)\n", "f2 = Fraction(8, 12)\n", "\n", "print(f'{f1.add(f2) = }')" ] }, { "cell_type": "markdown", "id": "9bd5e9a4-41ed-4353-86d4-b5565c4d8762", "metadata": {}, "source": [ "#### But it would be more natural to use the `+` operator." ] }, { "cell_type": "code", "execution_count": null, "id": "e5c6c7b4-de13-4279-929d-a629c327a201", "metadata": {}, "outputs": [], "source": [ "print(f'{f1 + f2 = }')" ] }, { "cell_type": "markdown", "id": "a629764c-9156-44dc-995a-e07feedb3618", "metadata": {}, "source": [ "### Testing" ] }, { "cell_type": "code", "execution_count": null, "id": "31189d40-365b-4e4d-b370-cb1a13545696", "metadata": { "tags": [] }, "outputs": [], "source": [ "f1 = Fraction(3, 4)\n", "print(f'{f1 = }')" ] }, { "cell_type": "code", "execution_count": null, "id": "34f2652a-8a20-4946-a825-33b57f83aab4", "metadata": {}, "outputs": [], "source": [ "f2 = Fraction(3, 9)\n", "print(f'{f2 = }')" ] }, { "cell_type": "code", "execution_count": null, "id": "2e4521a5-7ff6-4cb6-9a3b-a36c4896f491", "metadata": {}, "outputs": [], "source": [ "print(f'{f1.add(f2) = }')" ] }, { "cell_type": "code", "execution_count": null, "id": "5fb9b402-ed87-489f-b394-efb944f9dbae", "metadata": {}, "outputs": [], "source": [ "print(f'{f1 + f2 = }')" ] }, { "cell_type": "code", "execution_count": null, "id": "424640a6-2158-4f5c-82be-0288ff85cab0", "metadata": {}, "outputs": [], "source": [ "print(f'{f1 - f2 = }')" ] }, { "cell_type": "code", "execution_count": null, "id": "004dc67b-af16-42db-9787-4bef93ccd9a6", "metadata": {}, "outputs": [], "source": [ "print(f'{f1 * f2 = }')" ] }, { "cell_type": "code", "execution_count": null, "id": "8cff5f0a-8642-4679-9d27-581a2ee1f01f", "metadata": {}, "outputs": [], "source": [ "print(f'{f1 / f2 = }')" ] }, { "cell_type": "code", "execution_count": null, "id": "d0a5c2d5-7743-4508-a842-d09d2a8a62dd", "metadata": {}, "outputs": [], "source": [ "f1 = Fraction(1, 3)\n", "f2 = Fraction(1, 2)\n", "\n", "print(f'{f1 - f2 = }')" ] }, { "cell_type": "code", "execution_count": null, "id": "7e05eccd-9eba-497f-af93-1ce4c431cb56", "metadata": {}, "outputs": [], "source": [ "print(f'{f1//f2 = }')" ] }, { "cell_type": "code", "execution_count": null, "id": "b8f2570e-d40c-489d-872b-37780756c3da", "metadata": {}, "outputs": [], "source": [ "# (C) Copyright 2024 by Ronald Mak" ] }, { "cell_type": "code", "execution_count": null, "id": "5bac2040-5dd9-44b9-acd5-3ad126f7a90e", "metadata": {}, "outputs": [], "source": [] } ], "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.11.5" } }, "nbformat": 4, "nbformat_minor": 5 }