Skip to content

Commit 82faad4

Browse files
authored
Merge pull request #69 from rsokl/oop_edits
Incorporate AJ's edits for Module 4... finally!
2 parents 32c9fc0 + 08314d0 commit 82faad4

File tree

3 files changed

+57
-16
lines changed

3 files changed

+57
-16
lines changed

Python/Module4_OOP/ClassInstances.ipynb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
"source": [
77
"# Instances of a Class\n",
88
"\n",
9-
"Thus far we have learned about the syntax for defining a new class of object, specifying its name, attributes, and methods (which are attributes that are functions). The resulting *class object* is the singular object that encapsulates our class definition. We seldom will want to pass around or manipulate this class object once it is created. Rather, we will want to use it to create individual *instances* of that class. To be more concrete, `list` is a class object (remember that \"class\" and \"type\" are synonymous) - it is the same sort of object that is produced when a `class` definition is executed. As you saw in [Module 2](http://www.pythonlikeyoumeanit.com/Module2_EssentialsOfPython/Basic_Objects.html#Lists), we can use this class object to create individual *instances* of the list class, each one containing its own sequence of items.\n",
9+
"Thus far we have learned about the syntax for defining a new class of object, specifying its name, attributes, and methods (which are attributes that are functions). Once we leave the scope of the class definition, a class object is formed - the resulting *class object* is the singular object that encapsulates our class definition. We seldom will want to pass around or manipulate this class object once it is created. Rather, we will want to use it to create individual *instances* of that class. To be more concrete, `list` is a class object (remember that \"class\" and \"type\" are synonymous) - it is the same sort of object that is produced when a `class` definition is executed. As you saw in [Module 2](http://www.pythonlikeyoumeanit.com/Module2_EssentialsOfPython/Basic_Objects.html#Lists), we can use this class object to create individual *instances* of the list class, each one containing its own sequence of items.\n",
1010
"\n",
1111
"```python\n",
1212
"# using the class object `list` to create list-instances\n",
13-
">>> list()\n",
13+
">>> list() # create a `list`-instance that is empty\n",
1414
"[]\n",
1515
"\n",
16-
">>> list((1, 2, 3))\n",
16+
">>> list((1, 2, 3)) # create a `list`-instance containing 1, 2, and 3\n",
1717
"[1, 2, 3]\n",
1818
"\n",
1919
"# `a` and `b` are distinct instances of the list class/type\n",
@@ -197,10 +197,12 @@
197197
">>> y1 = y\n",
198198
"```\n",
199199
"\n",
200-
"What relationship do `x` and `y` share?\n",
201-
"\n",
202200
"What relationship do `x` and `Cat` share?\n",
203201
"\n",
202+
"What relationship do `y` and `Cat` share?\n",
203+
"\n",
204+
"What relationship do `x` and `y` share?\n",
205+
"\n",
204206
"What relationship do `x2` and `y` share?\n",
205207
"\n",
206208
"What relationship do `y` and `y1` share?\n",
@@ -432,11 +434,11 @@
432434
">>> y1 = y\n",
433435
"```\n",
434436
"\n",
435-
"What relationship do `Cat` and `y` share?: `y` is in instance of `Cat`.\n",
437+
"What relationship do `x` and `Cat` share?: `x` and `Cat` reference the same class object.\n",
436438
"\n",
437-
"What relationship do `x` and `y` share?: `x` references `Cat`, and `y` is an instance of `Cat`.\n",
439+
"What relationship do `y` and `Cat` share?: `y` is an instance of the `Cat` class.\n",
438440
"\n",
439-
"What relationship do `x` and `Cat` share?: `x` and `Cat` reference the same class object.\n",
441+
"What relationship do `x` and `y` share?: `x` references `Cat`, and `y` is an instance of `Cat`. Thus `y` is an instance of `x`.\n",
440442
"\n",
441443
"What relationship do `x2` and `y` share?: They are independent instances of `Cat`\n",
442444
"\n",
@@ -445,7 +447,7 @@
445447
"\n",
446448
"Identify each of the following objects as a class object or an instance (and if so, an instance of what)\n",
447449
"\n",
448-
" - `\"hello world\"`: An instances of the `str` type (a.k.a class)\n",
450+
" - `\"hello world\"`: An instance of the `str` type (a.k.a class)\n",
449451
" - `True`: an instance of the `bool` type\n",
450452
" - `int`: a class object describing integers\n",
451453
" - `{\"a\" : 22}`: an instance of the `dict` type \n",
@@ -496,7 +498,7 @@
496498
"name": "python",
497499
"nbconvert_exporter": "python",
498500
"pygments_lexer": "ipython3",
499-
"version": "3.6.3"
501+
"version": "3.6.7"
500502
}
501503
},
502504
"nbformat": 4,

Python/Module4_OOP/Introduction_to_OOP.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"```python\n",
5050
"class Rectangle:\n",
5151
" \"\"\" A Python object that describes the properties of a rectangle \"\"\"\n",
52-
" def __init__(self, width, height, center=(0, 0)):\n",
52+
" def __init__(self, width, height, center=(0.0, 0.0)):\n",
5353
" \"\"\" Sets the attributes of a particular instance of `Rectangle`.\n",
5454
"\n",
5555
" Parameters\n",
@@ -89,8 +89,8 @@
8989
" -------\n",
9090
" List[Tuple[float, float], Tuple[float, float], Tuple[float, float], Tuple[float, float]]\"\"\"\n",
9191
" cx, cy = self.center\n",
92-
" dx = self.width / 2\n",
93-
" dy = self.height / 2\n",
92+
" dx = self.width / 2.0\n",
93+
" dy = self.height / 2.0\n",
9494
" return [(cx + x, cy + y) for x,y in ((dx, dy), (dx, -dy), (-dx, -dy), (-dx, dy))]\n",
9595
"```\n",
9696
"\n",
@@ -127,7 +127,7 @@
127127
"source": [
128128
"Just like any other Python object that we have encountered, we can put our `Rectangle`s in lists, store them as values in dictionaries, pass them to functions, reference them with multiple variables, and so on.\n",
129129
"\n",
130-
"Popular STEM, data analysis, and machine learning Python libraries rely heavily on the ability to define custom classes of Python objects. For example, [pandas](https://pandas.pydata.org/) defines a spreadsheet-like `DataFrame` class; [PyTorch](https://pytorch.org/), [MXNet](https://mxnet.incubator.apache.org/), and [TensorFlow](https://www.tensorflow.org/) each define tensor classes that are capable of [automatic differentiation](https://en.wikipedia.org/wiki/Automatic_differentiation), which is critically important for training neural networks. Understanding Python's class system will greatly improve your ability to leverage libraries like these (Shameless plug: refer to [MyGrad](https://github.com/rsokl/MyGrad) if you are interested in seeing a simple pure-Python/NumPy implementation of an auto-differentiation library). \n",
130+
"Popular STEM, data analysis, and machine learning Python libraries rely heavily on the ability to define custom classes of Python objects. For example, [pandas](https://pandas.pydata.org/) defines a spreadsheet-like `DataFrame` class; [PyTorch](https://pytorch.org/), [MXNet](https://mxnet.incubator.apache.org/), and [TensorFlow](https://www.tensorflow.org/) each define tensor classes that are capable of [automatic differentiation](https://en.wikipedia.org/wiki/Automatic_differentiation), which is critically important for training neural networks. Understanding Python's class system will greatly improve your ability to leverage libraries like these (Shameless plug: refer to [MyGrad](https://mygrad.readthedocs.io) if you are interested in seeing a simple pure-Python/NumPy implementation of an auto-differentiation library). \n",
131131
"\n",
132132
"Moving forward, we will discuss the essential *class definition*, which will permit us to define our own class (a.k.a. type) of object. Next, we will learn about creating distinct *instances* of a given object type and about defining methods. This will lead to our first encounter with *special methods*, which enable us to affect how our object type behaves with Python's various operators. For example, we can define how the `+` operator interacts with our objects. Lastly, we will briefly discuss the concept of class inheritance. \n",
133133
"\n",
@@ -184,7 +184,7 @@
184184
"name": "python",
185185
"nbconvert_exporter": "python",
186186
"pygments_lexer": "ipython3",
187-
"version": "3.6.3"
187+
"version": "3.6.7"
188188
}
189189
},
190190
"nbformat": 4,

Python/Module4_OOP/Methods.ipynb

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,45 @@
194194
"```"
195195
]
196196
},
197+
{
198+
"cell_type": "markdown",
199+
"metadata": {},
200+
"source": [
201+
"`dict.fromkeys` is an example of a class method that takes in an iterable, and returns a dictionary whose keys are the elements of that iterable, and whose values all default to `None`.\n",
202+
"\n",
203+
"```python\n",
204+
">>> dict.fromkeys(\"abcd\")\n",
205+
"{'a': 2.3, 'b': 2.3, 'c': 2.3, 'd': 2.3}\n",
206+
"```\n",
207+
"\n",
208+
"It is sensible that this is a class method rather than an instance method, as the method creates a brand new dictionary from scratch. It need only have access to the `dict` object (i.e. the `cls` argument) so that it can construct the dictionary. The following is what an implementation of `fromkeys` could look like, were we to define `dict` ourselves: \n",
209+
"\n",
210+
"```python\n",
211+
"class dict:\n",
212+
" # assume all other dictionary methods are defined here\n",
213+
" @classmethod\n",
214+
" def fromkeys(cls, iterable, value=None):\n",
215+
" \"\"\" Creates a dictionary whose keys are the elements of `iterable`. All \n",
216+
" keys map to `value`.\n",
217+
" \n",
218+
" Parameters\n",
219+
" ----------\n",
220+
" iterable: Iterable[Hashable]\n",
221+
" An iterable of valid dictionary keys (i.e. any object that is hashable).\n",
222+
" \n",
223+
" value : Optional[Any]\n",
224+
" The value that all of the keys will map to. Defaults to `None`.\n",
225+
" \n",
226+
" Returns\n",
227+
" -------\n",
228+
" dict \"\"\"\n",
229+
" new_dict = cls() # equivalent to `dict()`: creates a new dictionary instance\n",
230+
" for key in iterable:\n",
231+
" new_dict[key] = value\n",
232+
" return new_dict\n",
233+
"```"
234+
]
235+
},
197236
{
198237
"cell_type": "markdown",
199238
"metadata": {},
@@ -283,7 +322,7 @@
283322
"name": "python",
284323
"nbconvert_exporter": "python",
285324
"pygments_lexer": "ipython3",
286-
"version": "3.6.5"
325+
"version": "3.6.7"
287326
}
288327
},
289328
"nbformat": 4,

0 commit comments

Comments
 (0)