Skip to content

Commit a5d445d

Browse files
author
Fred Baptiste
committed
Part 4/ Section 4 - Polymorphism and Special Methods
1 parent a011a09 commit a5d445d

8 files changed

+6806
-0
lines changed

Part 4/Section 04 - Polymorphism and Special Methods/01 - __str__ and __repr__ Methods.ipynb

Lines changed: 703 additions & 0 deletions
Large diffs are not rendered by default.

Part 4/Section 04 - Polymorphism and Special Methods/02 - Arithmetic Operators.ipynb

Lines changed: 1528 additions & 0 deletions
Large diffs are not rendered by default.

Part 4/Section 04 - Polymorphism and Special Methods/03 - Rich Comparisons.ipynb

Lines changed: 922 additions & 0 deletions
Large diffs are not rendered by default.

Part 4/Section 04 - Polymorphism and Special Methods/04 - Hashing and Equality.ipynb

Lines changed: 415 additions & 0 deletions
Large diffs are not rendered by default.

Part 4/Section 04 - Polymorphism and Special Methods/05 - Booleans.ipynb

Lines changed: 609 additions & 0 deletions
Large diffs are not rendered by default.

Part 4/Section 04 - Polymorphism and Special Methods/06 - Callables.ipynb

Lines changed: 1530 additions & 0 deletions
Large diffs are not rendered by default.

Part 4/Section 04 - Polymorphism and Special Methods/07 - The __del__ Method.ipynb

Lines changed: 781 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 318 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"### The `__format__` Method"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"We saw before the use of `__str__` and `__repr__`."
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"However we have one more formatting function to look at!\n",
22+
"\n",
23+
"The `format()` function."
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"For example we can use `format()` with a format specification for floats:"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": 1,
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"a = 0.1"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 2,
45+
"metadata": {},
46+
"outputs": [
47+
{
48+
"data": {
49+
"text/plain": [
50+
"'0.10000000000000000555'"
51+
]
52+
},
53+
"execution_count": 2,
54+
"metadata": {},
55+
"output_type": "execute_result"
56+
}
57+
],
58+
"source": [
59+
"format(a, '.20f')"
60+
]
61+
},
62+
{
63+
"cell_type": "markdown",
64+
"metadata": {},
65+
"source": [
66+
"Or we can use it with a datetime object:"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": 3,
72+
"metadata": {},
73+
"outputs": [],
74+
"source": [
75+
"from datetime import datetime"
76+
]
77+
},
78+
{
79+
"cell_type": "code",
80+
"execution_count": 4,
81+
"metadata": {},
82+
"outputs": [],
83+
"source": [
84+
"now = datetime.utcnow()"
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": 5,
90+
"metadata": {},
91+
"outputs": [
92+
{
93+
"data": {
94+
"text/plain": [
95+
"datetime.datetime(2019, 6, 13, 3, 43, 48, 904829)"
96+
]
97+
},
98+
"execution_count": 5,
99+
"metadata": {},
100+
"output_type": "execute_result"
101+
}
102+
],
103+
"source": [
104+
"now"
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": 6,
110+
"metadata": {},
111+
"outputs": [
112+
{
113+
"data": {
114+
"text/plain": [
115+
"'Thu 2019-06-13 03:43 AM'"
116+
]
117+
},
118+
"execution_count": 6,
119+
"metadata": {},
120+
"output_type": "execute_result"
121+
}
122+
],
123+
"source": [
124+
"format(now, '%a %Y-%m-%d %I:%M %p')"
125+
]
126+
},
127+
{
128+
"cell_type": "markdown",
129+
"metadata": {},
130+
"source": [
131+
"We can implement support for format specifiers in our own classes by implementing the `__format__` method.\n",
132+
"\n",
133+
"This is actually quite complicated to do, so we usually delegate back to some other type's formatting.\n",
134+
"\n",
135+
"Just like with `__str__` and `__repr__`, `__format__` should return a string."
136+
]
137+
},
138+
{
139+
"cell_type": "code",
140+
"execution_count": 7,
141+
"metadata": {},
142+
"outputs": [],
143+
"source": [
144+
"class Person:\n",
145+
" def __init__(self, name, dob):\n",
146+
" self.name = name\n",
147+
" self.dob = dob\n",
148+
" \n",
149+
" def __repr__(self):\n",
150+
" print('__repr__ called...')\n",
151+
" return f'Person(name={self.name}, dob={self.dob.isoformat()})'\n",
152+
" \n",
153+
" def __str__(self):\n",
154+
" print('__str__ called...')\n",
155+
" return f'Person({self.name})'\n",
156+
" \n",
157+
" def __format__(self, date_format_spec):\n",
158+
" print(f'__format__ called with {repr(date_format_spec)}...')\n",
159+
" dob = format(self.dob, date_format_spec)\n",
160+
" return f'Person(name={self.name}, dob={dob})'"
161+
]
162+
},
163+
{
164+
"cell_type": "markdown",
165+
"metadata": {},
166+
"source": [
167+
"So now have:"
168+
]
169+
},
170+
{
171+
"cell_type": "code",
172+
"execution_count": 8,
173+
"metadata": {},
174+
"outputs": [],
175+
"source": [
176+
"from datetime import date\n",
177+
"\n",
178+
"p = Person('Alex', date(1900, 10, 20))"
179+
]
180+
},
181+
{
182+
"cell_type": "code",
183+
"execution_count": 9,
184+
"metadata": {},
185+
"outputs": [
186+
{
187+
"name": "stdout",
188+
"output_type": "stream",
189+
"text": [
190+
"__str__ called...\n"
191+
]
192+
},
193+
{
194+
"data": {
195+
"text/plain": [
196+
"'Person(Alex)'"
197+
]
198+
},
199+
"execution_count": 9,
200+
"metadata": {},
201+
"output_type": "execute_result"
202+
}
203+
],
204+
"source": [
205+
"str(p)"
206+
]
207+
},
208+
{
209+
"cell_type": "code",
210+
"execution_count": 10,
211+
"metadata": {},
212+
"outputs": [
213+
{
214+
"name": "stdout",
215+
"output_type": "stream",
216+
"text": [
217+
"__repr__ called...\n"
218+
]
219+
},
220+
{
221+
"data": {
222+
"text/plain": [
223+
"'Person(name=Alex, dob=1900-10-20)'"
224+
]
225+
},
226+
"execution_count": 10,
227+
"metadata": {},
228+
"output_type": "execute_result"
229+
}
230+
],
231+
"source": [
232+
"repr(p)"
233+
]
234+
},
235+
{
236+
"cell_type": "code",
237+
"execution_count": 11,
238+
"metadata": {},
239+
"outputs": [
240+
{
241+
"name": "stdout",
242+
"output_type": "stream",
243+
"text": [
244+
"__format__ called with '%B %d, %Y'...\n"
245+
]
246+
},
247+
{
248+
"data": {
249+
"text/plain": [
250+
"'Person(name=Alex, dob=October 20, 1900)'"
251+
]
252+
},
253+
"execution_count": 11,
254+
"metadata": {},
255+
"output_type": "execute_result"
256+
}
257+
],
258+
"source": [
259+
"format(p, '%B %d, %Y')"
260+
]
261+
},
262+
{
263+
"cell_type": "markdown",
264+
"metadata": {},
265+
"source": [
266+
"If we do not specify a format, then the `format` function will use an empty string:"
267+
]
268+
},
269+
{
270+
"cell_type": "code",
271+
"execution_count": 12,
272+
"metadata": {},
273+
"outputs": [
274+
{
275+
"name": "stdout",
276+
"output_type": "stream",
277+
"text": [
278+
"__format__ called with ''...\n"
279+
]
280+
},
281+
{
282+
"data": {
283+
"text/plain": [
284+
"'Person(name=Alex, dob=1900-10-20)'"
285+
]
286+
},
287+
"execution_count": 12,
288+
"metadata": {},
289+
"output_type": "execute_result"
290+
}
291+
],
292+
"source": [
293+
"format(p)"
294+
]
295+
}
296+
],
297+
"metadata": {
298+
"kernelspec": {
299+
"display_name": "Python 3",
300+
"language": "python",
301+
"name": "python3"
302+
},
303+
"language_info": {
304+
"codemirror_mode": {
305+
"name": "ipython",
306+
"version": 3
307+
},
308+
"file_extension": ".py",
309+
"mimetype": "text/x-python",
310+
"name": "python",
311+
"nbconvert_exporter": "python",
312+
"pygments_lexer": "ipython3",
313+
"version": "3.6.7"
314+
}
315+
},
316+
"nbformat": 4,
317+
"nbformat_minor": 2
318+
}

0 commit comments

Comments
 (0)