|
4 | 4 | "cell_type": "markdown",
|
5 | 5 | "metadata": {},
|
6 | 6 | "source": [
|
7 |
| - "# Overloading" |
| 7 | + "# Introduction" |
8 | 8 | ]
|
9 | 9 | },
|
10 | 10 | {
|
|
14 | 14 | "Although Python doesn't support function overloading directly, there is functionality for it via its standard library in the `functools` module."
|
15 | 15 | ]
|
16 | 16 | },
|
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "metadata": {}, |
| 20 | + "source": [ |
| 21 | + "# Requirements" |
| 22 | + ] |
| 23 | + }, |
17 | 24 | {
|
18 | 25 | "cell_type": "code",
|
19 |
| - "execution_count": 1, |
| 26 | + "execution_count": 39, |
20 | 27 | "metadata": {},
|
21 | 28 | "outputs": [],
|
22 | 29 | "source": [
|
|
27 | 34 | "cell_type": "markdown",
|
28 | 35 | "metadata": {},
|
29 | 36 | "source": [
|
30 |
| - "## Simple example" |
| 37 | + "# Simple example" |
31 | 38 | ]
|
32 | 39 | },
|
33 | 40 | {
|
|
132 | 139 | "source": [
|
133 | 140 | "my_func(['a', 'b', 'c'], True)"
|
134 | 141 | ]
|
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "markdown", |
| 145 | + "metadata": {}, |
| 146 | + "source": [ |
| 147 | + "# Overloaded constructor example" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "markdown", |
| 152 | + "metadata": {}, |
| 153 | + "source": [ |
| 154 | + "For object methods, `functools` has the `singledispatchmethod` decorator. The following class has an overloaded constructor that takes either two `float` values, a `Point` object, or a `tuple` to initialize the $x$ and $y$ coordinates of the point." |
| 155 | + ] |
| 156 | + }, |
| 157 | + { |
| 158 | + "cell_type": "code", |
| 159 | + "execution_count": 78, |
| 160 | + "metadata": {}, |
| 161 | + "outputs": [], |
| 162 | + "source": [ |
| 163 | + "class Point():\n", |
| 164 | + " \n", |
| 165 | + " @functools.singledispatchmethod\n", |
| 166 | + " def __init__(self):\n", |
| 167 | + " raise NotImplementedError\n", |
| 168 | + "\n", |
| 169 | + " @__init__.register\n", |
| 170 | + " def _(self, x: float, y: float):\n", |
| 171 | + " self._x, self._y = x, y\n", |
| 172 | + " \n", |
| 173 | + " @property\n", |
| 174 | + " def x(self):\n", |
| 175 | + " return self._x\n", |
| 176 | + " \n", |
| 177 | + " @property\n", |
| 178 | + " def y(self):\n", |
| 179 | + " return self._y\n", |
| 180 | + " \n", |
| 181 | + " def __repr__(self):\n", |
| 182 | + " return f'({self.x}, {self.y})'\n", |
| 183 | + "\n", |
| 184 | + "@Point.__init__.register\n", |
| 185 | + "def _(self, other: Point):\n", |
| 186 | + " self._x, self._y = other.x, other.y\n", |
| 187 | + " \n", |
| 188 | + "@Point.__init__.register\n", |
| 189 | + "def _(self, coords: tuple):\n", |
| 190 | + " self._x, self._y = coords" |
| 191 | + ] |
| 192 | + }, |
| 193 | + { |
| 194 | + "cell_type": "markdown", |
| 195 | + "metadata": {}, |
| 196 | + "source": [ |
| 197 | + "Note that `tuple[float, float]` will not work since `issubclass` and `isinstance` do not accept parameterized generic types as their second arguments." |
| 198 | + ] |
| 199 | + }, |
| 200 | + { |
| 201 | + "cell_type": "markdown", |
| 202 | + "metadata": {}, |
| 203 | + "source": [ |
| 204 | + "Specifying the $x$ and $y$ coordinates of the new point." |
| 205 | + ] |
| 206 | + }, |
| 207 | + { |
| 208 | + "cell_type": "code", |
| 209 | + "execution_count": 79, |
| 210 | + "metadata": {}, |
| 211 | + "outputs": [ |
| 212 | + { |
| 213 | + "data": { |
| 214 | + "text/plain": [ |
| 215 | + "(3.1, 5.3)" |
| 216 | + ] |
| 217 | + }, |
| 218 | + "execution_count": 79, |
| 219 | + "metadata": {}, |
| 220 | + "output_type": "execute_result" |
| 221 | + } |
| 222 | + ], |
| 223 | + "source": [ |
| 224 | + "p = Point(3.1, 5.3)\n", |
| 225 | + "p" |
| 226 | + ] |
| 227 | + }, |
| 228 | + { |
| 229 | + "cell_type": "markdown", |
| 230 | + "metadata": {}, |
| 231 | + "source": [ |
| 232 | + "Using an existing point to create a new one (copy constructor)." |
| 233 | + ] |
| 234 | + }, |
| 235 | + { |
| 236 | + "cell_type": "code", |
| 237 | + "execution_count": 80, |
| 238 | + "metadata": {}, |
| 239 | + "outputs": [ |
| 240 | + { |
| 241 | + "data": { |
| 242 | + "text/plain": [ |
| 243 | + "(3.1, 5.3)" |
| 244 | + ] |
| 245 | + }, |
| 246 | + "execution_count": 80, |
| 247 | + "metadata": {}, |
| 248 | + "output_type": "execute_result" |
| 249 | + } |
| 250 | + ], |
| 251 | + "source": [ |
| 252 | + "p2 = Point(p)\n", |
| 253 | + "p2" |
| 254 | + ] |
| 255 | + }, |
| 256 | + { |
| 257 | + "cell_type": "markdown", |
| 258 | + "metadata": {}, |
| 259 | + "source": [ |
| 260 | + "Specifying the coordinates as a tuple." |
| 261 | + ] |
| 262 | + }, |
| 263 | + { |
| 264 | + "cell_type": "code", |
| 265 | + "execution_count": 83, |
| 266 | + "metadata": {}, |
| 267 | + "outputs": [ |
| 268 | + { |
| 269 | + "data": { |
| 270 | + "text/plain": [ |
| 271 | + "(3.5, 9.3)" |
| 272 | + ] |
| 273 | + }, |
| 274 | + "execution_count": 83, |
| 275 | + "metadata": {}, |
| 276 | + "output_type": "execute_result" |
| 277 | + } |
| 278 | + ], |
| 279 | + "source": [ |
| 280 | + "p3 = Point((3.5, 9.3))\n", |
| 281 | + "p3" |
| 282 | + ] |
135 | 283 | }
|
136 | 284 | ],
|
137 | 285 | "metadata": {
|
|
150 | 298 | "name": "python",
|
151 | 299 | "nbconvert_exporter": "python",
|
152 | 300 | "pygments_lexer": "ipython3",
|
153 |
| - "version": "3.7.5" |
| 301 | + "version": "3.9.5" |
154 | 302 | }
|
155 | 303 | },
|
156 | 304 | "nbformat": 4,
|
|
0 commit comments