|
19 | 19 | "cell_type": "markdown", |
20 | 20 | "metadata": {}, |
21 | 21 | "source": [ |
22 | | - "It is time to learn how to define your own type! " |
| 22 | + "You encountered several data types. Sometimes you may need a custom type. So it is time to learn how to define your own data type! " |
23 | 23 | ] |
24 | 24 | }, |
25 | 25 | { |
26 | 26 | "cell_type": "markdown", |
27 | 27 | "metadata": {}, |
28 | 28 | "source": [ |
29 | | - "Creating your own type is a bit more complicated than writing functions, but it comes with big advantages that will be apparent soon." |
| 29 | + "Creating your own data type is a bit more complicated than writing functions, but it comes with big advantages that soon will be apparent." |
30 | 30 | ] |
31 | 31 | }, |
32 | 32 | { |
|
58 | 58 | "cell_type": "markdown", |
59 | 59 | "metadata": {}, |
60 | 60 | "source": [ |
61 | | - "However, this notebook will only use a class as a data container. And we will learn it by example. " |
| 61 | + "This notebook will **only** use a class as a data container. And you will learn by example. " |
62 | 62 | ] |
63 | 63 | }, |
64 | 64 | { |
65 | 65 | "cell_type": "markdown", |
66 | 66 | "metadata": {}, |
67 | 67 | "source": [ |
68 | | - "## Class Definition and Instantiation" |
| 68 | + "## Class Definition" |
69 | 69 | ] |
70 | 70 | }, |
71 | 71 | { |
72 | 72 | "cell_type": "markdown", |
73 | 73 | "metadata": {}, |
74 | 74 | "source": [ |
75 | | - "Assuming that we want to create a class that is able to hold the content of the `sal.txt` file cited in a [previous notebook](006_Read_and_Write_Text_Files.ipynb). As such, we will call this new class `SalinityData`." |
| 75 | + "We want to create a class that is able to hold the content of the `sal.txt` file cited in a [previous notebook](006_Read_and_Write_Text_Files.ipynb). We will give this class the meaningful name of `SalinityData`." |
76 | 76 | ] |
77 | 77 | }, |
78 | 78 | { |
|
96 | 96 | "cell_type": "markdown", |
97 | 97 | "metadata": {}, |
98 | 98 | "source": [ |
99 | | - "The first line of the above code contains: the `class` keyword, the class name (`SalinityData`), and a `:`." |
| 99 | + "The first line of the above code contains: the `class` keyword, the class name (`SalinityData`), and a `:`" |
100 | 100 | ] |
101 | 101 | }, |
102 | 102 | { |
103 | 103 | "cell_type": "markdown", |
104 | 104 | "metadata": {}, |
105 | 105 | "source": [ |
106 | | - "The second line is a special text, called `docstring`, that provides a brief description of the class. The description is between `\"\"\" \"\"\"` (i.e., triple quotes)." |
| 106 | + "The second line is a special descriptive text, called `docstring`, that provides a brief explanation about the class. The description is between `\"\"\" \"\"\"` (i.e., triple quotes)." |
107 | 107 | ] |
108 | 108 | }, |
109 | 109 | { |
|
119 | 119 | "cell_type": "markdown", |
120 | 120 | "metadata": {}, |
121 | 121 | "source": [ |
122 | | - "Once defined, we can create an instance of the `SalinityData` class, by calling it with `()` at the end:" |
| 122 | + "Once defined, we can create an object (**instance**) of the `SalinityData` class, by calling it with `()` at the end:" |
123 | 123 | ] |
124 | 124 | }, |
125 | 125 | { |
|
162 | 162 | "cell_type": "markdown", |
163 | 163 | "metadata": {}, |
164 | 164 | "source": [ |
165 | | - "Because the `SalinityData` class is defined in this \"main\" notebook, the full name of the type is `__main__.SalinityData`" |
| 165 | + "Because the `SalinityData` class is defined in this notebook, the full name of the type is `__main__.SalinityData`. At this stage of learning Python, you can safely ignore the `__main__.` part." |
166 | 166 | ] |
167 | 167 | }, |
168 | 168 | { |
169 | 169 | "cell_type": "markdown", |
170 | 170 | "metadata": {}, |
171 | 171 | "source": [ |
172 | | - "At the moment, this new defined class is not that exciting. We will soon start to populate it with a number of useful **attributes** and **methods**." |
| 172 | + "At the moment, this newly defined class is not that exciting. We will soon start to populate it with a number of useful **attributes** and **methods**." |
173 | 173 | ] |
174 | 174 | }, |
175 | 175 | { |
|
178 | 178 | "source": [ |
179 | 179 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n", |
180 | 180 | "\n", |
181 | | - "The **class attributes** are used to maintain the state of each class instance. The **class methods** may modify the attributes and thus its state." |
| 181 | + "The **class attributes** are variables that capture all the information describing the class. Thus, **class attributes** maintain the **state** of each class instance. The **class methods** may modify the attributes and thus its state." |
182 | 182 | ] |
183 | 183 | }, |
184 | 184 | { |
|
187 | 187 | "source": [ |
188 | 188 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n", |
189 | 189 | "\n", |
190 | | - "The **methods** definition **always** has `self` as first parameter. The `self` is absent in the generic **functions** that we have seen in the [Write Your Own Functions notebook](005_Write_Your_Own_Functions.ipynb)." |
| 190 | + "The **class methods** definition must **always** have `self` as first parameter." |
191 | 191 | ] |
192 | 192 | }, |
193 | 193 | { |
|
203 | 203 | "source": [ |
204 | 204 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/key.png\">\n", |
205 | 205 | "\n", |
206 | | - "The **class initialization** happens within a special method called `__init__(self)`. " |
| 206 | + "The **class initialization** is done by a special method called `__init__(self)`. " |
207 | 207 | ] |
208 | 208 | }, |
209 | 209 | { |
210 | 210 | "cell_type": "markdown", |
211 | 211 | "metadata": {}, |
212 | 212 | "source": [ |
213 | | - "If you don't provide a `__init__(self)` method (like in the above code), Python will create an implicit one for you." |
| 213 | + "If you don't provide a `__init__(self)` method (like in the code below), Python will create one for you behind the scenes (**implicitly**)." |
214 | 214 | ] |
215 | 215 | }, |
216 | 216 | { |
|
262 | 262 | }, |
263 | 263 | { |
264 | 264 | "cell_type": "code", |
265 | | - "execution_count": 13, |
266 | | - "metadata": {}, |
267 | | - "outputs": [ |
268 | | - { |
269 | | - "name": "stdout", |
270 | | - "output_type": "stream", |
271 | | - "text": [ |
272 | | - "Salinity values: []\n" |
273 | | - ] |
274 | | - } |
275 | | - ], |
| 265 | + "execution_count": null, |
| 266 | + "metadata": {}, |
| 267 | + "outputs": [], |
276 | 268 | "source": [ |
277 | 269 | "sal_data = SalinityData()\n", |
278 | | - "print(\"Salinity values: %s\" % (sal_data.sal_values, ))" |
| 270 | + "print(\"Salinity values: %s\" % (sal_data.sal_values))" |
279 | 271 | ] |
280 | 272 | }, |
281 | 273 | { |
282 | 274 | "cell_type": "markdown", |
283 | 275 | "metadata": {}, |
284 | 276 | "source": [ |
285 | | - "The above code prints that the list content is `[]`. This is not surprising since the list was created empty in the `__init__(self)` method." |
| 277 | + "The above code prints the list content as `[]`. This is not surprising since an empty list was created in the `__init__(self)` method." |
286 | 278 | ] |
287 | 279 | }, |
288 | 280 | { |
|
308 | 300 | "source": [ |
309 | 301 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/test.png\">\n", |
310 | 302 | "\n", |
311 | | - "Write a class for a `TemperatureData` with functionalities similar to the above `SalinityData`. Then, add the code to demonstrate its functionalities." |
| 303 | + "Write a `TemperatureData` class definition similar to the above `SalinityData`. Then, create an instance of that class and print the temperature values." |
312 | 304 | ] |
313 | 305 | }, |
314 | 306 | { |
|
348 | 340 | "cell_type": "markdown", |
349 | 341 | "metadata": {}, |
350 | 342 | "source": [ |
351 | | - "There is much more that you can do with a class, but this is the amount of information that you need to complete this training. " |
| 343 | + "There is much more that you can do with a class, but this is the amount of information that you need to complete this *Programming Basics with Python* training. " |
352 | 344 | ] |
353 | 345 | }, |
354 | 346 | { |
|
357 | 349 | "source": [ |
358 | 350 | "<img align=\"left\" width=\"6%\" style=\"padding-right:10px;\" src=\"images/info.png\">\n", |
359 | 351 | "\n", |
360 | | - "If you are eager to know more about classes, you may read [this supplemental notebook](SUP_First_Steps_of_a_Class.ipynb)." |
| 352 | + "If you are interested in learning more about classes, you may read [this supplemental notebook](SUP_First_Steps_of_a_Class.ipynb)." |
361 | 353 | ] |
362 | 354 | }, |
363 | 355 | { |
|
398 | 390 | "metadata": {}, |
399 | 391 | "source": [ |
400 | 392 | "<!--NAVIGATION-->\n", |
401 | | - "[< Dictionaries and Metadata](007_Dictionaries_and_Metadata.ipynb) | [Contents](index.ipynb) | [Wrapping Up Notions >](009_Wrapping_Up_Notions.ipynb)" |
| 393 | + "[< Dictionaries and Metadata](007_Dictionaries_and_Metadata.ipynb) | [Contents](index.ipynb) | [Summing-Up >](009_Summing-Up.ipynb)" |
402 | 394 | ] |
403 | 395 | } |
404 | 396 | ], |
|
0 commit comments