Skip to content

Commit 2434b8b

Browse files
committed
Add singledispatchmehtod example
1 parent 3726d01 commit 2434b8b

File tree

1 file changed

+152
-4
lines changed

1 file changed

+152
-4
lines changed

source-code/object-orientation/overloading.ipynb

Lines changed: 152 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"# Overloading"
7+
"# Introduction"
88
]
99
},
1010
{
@@ -14,9 +14,16 @@
1414
"Although Python doesn't support function overloading directly, there is functionality for it via its standard library in the `functools` module."
1515
]
1616
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"# Requirements"
22+
]
23+
},
1724
{
1825
"cell_type": "code",
19-
"execution_count": 1,
26+
"execution_count": 39,
2027
"metadata": {},
2128
"outputs": [],
2229
"source": [
@@ -27,7 +34,7 @@
2734
"cell_type": "markdown",
2835
"metadata": {},
2936
"source": [
30-
"## Simple example"
37+
"# Simple example"
3138
]
3239
},
3340
{
@@ -132,6 +139,147 @@
132139
"source": [
133140
"my_func(['a', 'b', 'c'], True)"
134141
]
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+
]
135283
}
136284
],
137285
"metadata": {
@@ -150,7 +298,7 @@
150298
"name": "python",
151299
"nbconvert_exporter": "python",
152300
"pygments_lexer": "ipython3",
153-
"version": "3.7.5"
301+
"version": "3.9.5"
154302
}
155303
},
156304
"nbformat": 4,

0 commit comments

Comments
 (0)