|
6 | 6 | "source": [ |
7 | 7 | "# Instances of a Class\n", |
8 | 8 | "\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", |
10 | 10 | "\n", |
11 | 11 | "```python\n", |
12 | 12 | "# using the class object `list` to create list-instances\n", |
13 | | - ">>> list()\n", |
| 13 | + ">>> list() # create a `list`-instance that is empty\n", |
14 | 14 | "[]\n", |
15 | 15 | "\n", |
16 | | - ">>> list((1, 2, 3))\n", |
| 16 | + ">>> list((1, 2, 3)) # create a `list`-instance containing 1, 2, and 3\n", |
17 | 17 | "[1, 2, 3]\n", |
18 | 18 | "\n", |
19 | 19 | "# `a` and `b` are distinct instances of the list class/type\n", |
|
197 | 197 | ">>> y1 = y\n", |
198 | 198 | "```\n", |
199 | 199 | "\n", |
200 | | - "What relationship do `x` and `y` share?\n", |
201 | | - "\n", |
202 | 200 | "What relationship do `x` and `Cat` share?\n", |
203 | 201 | "\n", |
| 202 | + "What relationship do `y` and `Cat` share?\n", |
| 203 | + "\n", |
| 204 | + "What relationship do `x` and `y` share?\n", |
| 205 | + "\n", |
204 | 206 | "What relationship do `x2` and `y` share?\n", |
205 | 207 | "\n", |
206 | 208 | "What relationship do `y` and `y1` share?\n", |
|
432 | 434 | ">>> y1 = y\n", |
433 | 435 | "```\n", |
434 | 436 | "\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", |
436 | 438 | "\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", |
438 | 440 | "\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", |
440 | 442 | "\n", |
441 | 443 | "What relationship do `x2` and `y` share?: They are independent instances of `Cat`\n", |
442 | 444 | "\n", |
|
445 | 447 | "\n", |
446 | 448 | "Identify each of the following objects as a class object or an instance (and if so, an instance of what)\n", |
447 | 449 | "\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", |
449 | 451 | " - `True`: an instance of the `bool` type\n", |
450 | 452 | " - `int`: a class object describing integers\n", |
451 | 453 | " - `{\"a\" : 22}`: an instance of the `dict` type \n", |
|
496 | 498 | "name": "python", |
497 | 499 | "nbconvert_exporter": "python", |
498 | 500 | "pygments_lexer": "ipython3", |
499 | | - "version": "3.6.3" |
| 501 | + "version": "3.6.7" |
500 | 502 | } |
501 | 503 | }, |
502 | 504 | "nbformat": 4, |
|
0 commit comments