1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
|
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>11. Introduction to Object-Oriented Programming</h1>\n",
"<h2>10/27/2023</h2>\n",
"\n",
"<h2>11.0 Last Time...</h2>\n",
"<ul>\n",
" <li>NetCDF is a powerful file type containing global attributes, variables, variable attributes, and dimensions.</li>\n",
" <li>We can read from NetCDF files using similar syntax to that for regular files.</li>\n",
" <li>Using attributes such as 'dimensions' and 'variables', we can learn about individual variables in the dataset.</li>\n",
" <li>We can also write to NetCDF files in a simlar way.</li>\n",
"</ul>\n",
"\n",
"<h2>11.1 What is Object-Oriented Programming?</h2>\n",
"\n",
"OOP is presented here in opposition to procedural programming. <b>Procedural</b> programs consider two entities: data and functions. Procedurally, the two things are different: a function will take data as input and return data as output (this should sound familiar!). There's nothing customizable about a function with respect to data, which means you can use functions on various types of data with no restrictions... which can you get into trouble.\n",
"\n",
"In reality, though, we tend to think of things as having both \"state\" and \"behavior\". People can have a state (tall, short, etc.) but also a behavior (playing basketball, running, etc.), and the two can happen simultaneously.\n",
"\n",
"Object-oriented programming attempts to imitate this approach, so specific objects in the code will have a state and a behavior attached to them.\n",
"\n",
"<h2>11.2 What is an Object?</h2>\n",
"\n",
"An object in programming has two entities attached to it: data... and the things that <i>act</i> on that data. The data are called <b>attributes</b>, and the functions attached to the object that can act on that data are called <b>methods</b>. \n",
"\n",
"These methods are specifically made to act on attributes; they aren't just random functions meant as one-size-fits-all solutions, which is what we would see in procedural programming.\n",
"\n",
"Objects are generally specific realizations of some <b>class</b> or <b>type</b>. As an example, individual people are specific realizations of the <b>class</b> of human beings. Specific realizations (instances) differ from each other in details but have the same overall pattern. In OOP, specific realizations are <b>instances</b> and common patterns are <b>classes</b>.\n",
"\n",
"<h2>11.3 How do Objects Work?</h2>\n",
"\n",
"In Python, strings (like almost everything in Python) are objects. Built into Python, there is a class called 'strings', and each time you make a new string, you're using that definition. Python implicitly defines attributes and methods for all string objects; no matter what string you create, you have that set of data and functions associated with your string."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['__add__',\n",
" '__class__',\n",
" '__contains__',\n",
" '__delattr__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__getitem__',\n",
" '__getnewargs__',\n",
" '__getstate__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__init_subclass__',\n",
" '__iter__',\n",
" '__le__',\n",
" '__len__',\n",
" '__lt__',\n",
" '__mod__',\n",
" '__mul__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__rmod__',\n",
" '__rmul__',\n",
" '__setattr__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" 'capitalize',\n",
" 'casefold',\n",
" 'center',\n",
" 'count',\n",
" 'encode',\n",
" 'endswith',\n",
" 'expandtabs',\n",
" 'find',\n",
" 'format',\n",
" 'format_map',\n",
" 'index',\n",
" 'isalnum',\n",
" 'isalpha',\n",
" 'isascii',\n",
" 'isdecimal',\n",
" 'isdigit',\n",
" 'isidentifier',\n",
" 'islower',\n",
" 'isnumeric',\n",
" 'isprintable',\n",
" 'isspace',\n",
" 'istitle',\n",
" 'isupper',\n",
" 'join',\n",
" 'ljust',\n",
" 'lower',\n",
" 'lstrip',\n",
" 'maketrans',\n",
" 'partition',\n",
" 'removeprefix',\n",
" 'removesuffix',\n",
" 'replace',\n",
" 'rfind',\n",
" 'rindex',\n",
" 'rjust',\n",
" 'rpartition',\n",
" 'rsplit',\n",
" 'rstrip',\n",
" 'split',\n",
" 'splitlines',\n",
" 'startswith',\n",
" 'strip',\n",
" 'swapcase',\n",
" 'title',\n",
" 'translate',\n",
" 'upper',\n",
" 'zfill']"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# The dir() command gives you a list of all the attributes and methods\n",
"# associated with a given object.\n",
"a = \"hello world\"\n",
"dir(a)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello World\n",
"HELLO WORLD\n"
]
}
],
"source": [
"# To refer to an attribute or method of an instance,\n",
"# you just add a period after the object name and then put\n",
"# the attribute or method name.\n",
"print(a.title())\n",
"print(a.upper())\n",
"# Methods can produce a return value, act on attributes of the object in-place,\n",
"# or both!"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"# isupper() will determine whether the object is in uppercase.\n",
"b = \"BALLS\"\n",
"print(b.isupper())\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n"
]
}
],
"source": [
"# To count the instances of a particular character, you can use count()\n",
"print(a.count(\"l\"))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As another example, let's consider how objects work for arrays!\n",
"\n",
"Arrays have attributes and methods built in to them just like any other object."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0. 1. 2.]\n",
" [ 3. 4. 5.]\n",
" [ 6. 7. 8.]\n",
" [ 9. 10. 11.]]\n"
]
}
],
"source": [
"import numpy as np\n",
"a = np.arange(12.)\n",
"a = np.reshape(a,(4,3))\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['T', '__abs__', '__add__', '__and__', '__array__', '__array_finalize__', '__array_function__', '__array_interface__', '__array_prepare__', '__array_priority__', '__array_struct__', '__array_ufunc__', '__array_wrap__', '__bool__', '__class__', '__class_getitem__', '__complex__', '__contains__', '__copy__', '__deepcopy__', '__delattr__', '__delitem__', '__dir__', '__divmod__', '__dlpack__', '__dlpack_device__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__iadd__', '__iand__', '__ifloordiv__', '__ilshift__', '__imatmul__', '__imod__', '__imul__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__ior__', '__ipow__', '__irshift__', '__isub__', '__iter__', '__itruediv__', '__ixor__', '__le__', '__len__', '__lshift__', '__lt__', '__matmul__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmatmul__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__setitem__', '__setstate__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__xor__', 'all', 'any', 'argmax', 'argmin', 'argpartition', 'argsort', 'astype', 'base', 'byteswap', 'choose', 'clip', 'compress', 'conj', 'conjugate', 'copy', 'ctypes', 'cumprod', 'cumsum', 'data', 'diagonal', 'dot', 'dtype', 'dump', 'dumps', 'fill', 'flags', 'flat', 'flatten', 'getfield', 'imag', 'item', 'itemset', 'itemsize', 'max', 'mean', 'min', 'nbytes', 'ndim', 'newbyteorder', 'nonzero', 'partition', 'prod', 'ptp', 'put', 'ravel', 'real', 'repeat', 'reshape', 'resize', 'round', 'searchsorted', 'setfield', 'setflags', 'shape', 'size', 'sort', 'squeeze', 'std', 'strides', 'sum', 'swapaxes', 'take', 'tobytes', 'tofile', 'tolist', 'tostring', 'trace', 'transpose', 'var', 'view']\n"
]
}
],
"source": [
"# Now let's look at all the attributes and methods!\n",
"print(dir(a))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(4, 3)\n"
]
}
],
"source": [
"# Any attributes with two underscores probably shouldn't\n",
"# be messed with! This is how Python decides what to do when\n",
"# you type '*' or '/'.\n",
"\n",
"# Some of the other interesting methods include:\n",
"\n",
"print(np.shape(a))"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0., 1., 3., 6., 10., 15., 21., 28., 36., 45., 55., 66.])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.cumsum()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0. 3. 6. 9.]\n",
" [ 1. 4. 7. 10.]\n",
" [ 2. 5. 8. 11.]]\n"
]
}
],
"source": [
"print(a.T)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0., 0., 0.],\n",
" [ 0., 0., 0.],\n",
" [10., 10., 10.],\n",
" [10., 10., 10.]])"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.round(-1)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a.ravel()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Practice Exercises!</b>"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The rain in Spain.\n",
"THE RAIN IN SPAIN.\n",
"True\n",
"3\n"
]
}
],
"source": [
"a = 'The rain in Spain.'\n",
"#1. Create a new string b that is a but all in uppercase.\n",
"b = a.upper()\n",
"#2. Is a changed when you create b?\n",
"print(a)\n",
"print(b)\n",
"# no?\n",
"#3. How would you test to see whether b is in uppercase? That is, how \n",
"# could you return a boolean that is True or False depending on whether \n",
"# b is uppercase?\n",
"print(b.isupper())\n",
"#4. How would you calculate the number of occurrences of the letter 'n' in a?\n",
"print(a.count(\"n\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Round 2!</b>"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[2.3 8. 3.2]\n",
" [4.3 0.4 4.3]\n",
" [1.2 0.3 5.4]\n",
" [4.3 5.6 6.5]]\n",
"[2.3 8. 3.2 4.3 0.4 4.3 1.2 0.3 5.4 4.3 5.6 6.5]\n",
"[[2.3 8. 3.2 4.3 0.4 4.3]\n",
" [1.2 0.3 5.4 4.3 5.6 6.5]]\n"
]
}
],
"source": [
"#1. Create a 3 column, 4 row array named a. The array can have any numerical values\n",
"# you want, as long as all the elements are not all identical.\n",
"x = np.array([[2.3,4.3,1.2,4.3],[8.0,0.4,0.3,5.6],[3.2,4.3,5.4,6.5]])\n",
"x = x.T\n",
"print(x)\n",
"#2. Create an array b that is a copy of a but is 1-D, not 2-D.\n",
"b = np.ravel(x)\n",
"print(b)\n",
"#3. Turn b into a 6 column, 2 row array.\n",
"b = np.reshape(b,(2,6))\n",
"#4. Create an array c where you round all elements of b to 1 decimal place.\n",
"c = b.round(1)\n",
"print(c)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>11.4 Take-Home Points</h2>\n",
"<ul>\n",
" <li><b>Objects</b> have attributes and methods associated with them that can be listed using <b>dir()</b>.</li>\n",
" <li>Methods for strings include <b>upper()</b>, <b>isupper()</b>, <b>count()</b>, <b>title()</b>, etc.</li>\n",
" <li>Methods for arrays include <b>reshape()</b>, <b>ravel()</b>, <b>round()</b>, etc.</li>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|