Created
October 26, 2019 00:20
-
-
Save simecek/8be0adf3d3f1cd0f82524229efeb4216 to your computer and use it in GitHub Desktop.
Reflected Operands
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
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Reflected Operands", | |
"provenance": [], | |
"collapsed_sections": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/simecek/8be0adf3d3f1cd0f82524229efeb4216/reflected-operands.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "aACvaBgTasj8", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"## A custom class for modular arithmetic" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "pCkxQ2XLNxpW", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"class Mod(object):\n", | |
" def __init__(self, value, mod):\n", | |
" self.value = value % mod\n", | |
" self.mod = mod\n", | |
" def __repr__(self):\n", | |
" return f'{self.value} (mod {self.mod})'\n", | |
" def __add__(self, i):\n", | |
" new_value = (self.value + i) % self.mod\n", | |
" return Mod(new_value, self.mod)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Mtz9VsAKPyH6", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "039f6abc-7f5d-48f9-a21e-0b3077f7ffd4" | |
}, | |
"source": [ | |
"# this works ok (calls Mod.__add__)\n", | |
"Mod(4, mod=7) + 4" | |
], | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"1 (mod 7)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 2 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "S7dZMvewP5Zs", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 166 | |
}, | |
"outputId": "f6793e7f-2d46-4619-d012-b54cd5d67d25" | |
}, | |
"source": [ | |
"# this throws an error because (calls int.__add__)\n", | |
"4 + Mod(4, mod=7)" | |
], | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "error", | |
"ename": "TypeError", | |
"evalue": "ignored", | |
"traceback": [ | |
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", | |
"\u001b[0;32m<ipython-input-3-24edf17886ea>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m4\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mMod\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmod\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'Mod'" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "aoekvVC_bXlt", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"### Corrected version (with `__radd__` method)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "QOMccLj4QzVV", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"class CorrectedMod(object):\n", | |
" def __init__(self, value, mod):\n", | |
" self.value = value % mod\n", | |
" self.mod = mod\n", | |
" def __repr__(self):\n", | |
" return f'{self.value} (mod {self.mod})'\n", | |
" def __add__(self, i):\n", | |
" new_value = (self.value + i) % self.mod\n", | |
" return CorrectedMod(new_value, self.mod)\n", | |
" def __radd__(self, i):\n", | |
" return self.__add__(i)" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "gf9SF3dPTFiY", | |
"colab_type": "code", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 35 | |
}, | |
"outputId": "05b3cab6-5bf1-4c74-894f-8a01f39ff70e" | |
}, | |
"source": [ | |
"4 + CorrectedMod(4, mod=7)" | |
], | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"1 (mod 7)" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 5 | |
} | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "gsppRjpcaG3t", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"Another example on Stack Overflow: \n", | |
"\n", | |
"https://stackoverflow.com/questions/41598203/overloading-bitwise-operators-to-work-with-booleans-in-python" | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment