Skip to content

Commit c9fa5b5

Browse files
committed
PCC Ch. 9 work
1 parent c807fe4 commit c9fa5b5

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

PCC_Ch-8_Notebook1.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2402,7 +2402,7 @@
24022402
"name": "python",
24032403
"nbconvert_exporter": "python",
24042404
"pygments_lexer": "ipython3",
2405-
"version": "3.9.7"
2405+
"version": "3.10.0"
24062406
},
24072407
"orig_nbformat": 4
24082408
},

PCC_Ch-9_Notebook1.ipynb

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Ch. 9: Classes\n",
8+
"\n",
9+
"In *object-oriented programming* you write *classes* that represebt real-world things and situations, and you create *objects* based on these classes. When you write a class, you define the general behavior that a whole category of objects can have.\n",
10+
"\n",
11+
"Making an object from a class if called *instantiation*, and you work with *instances* of a class.\n",
12+
"- In this chapter you’ll write classes and create instances of those classes. You’ll specify the kind of information that can be stored in instances, and you’ll define actions that can be taken with these instances. You’ll also write classes that extend the functionality of existing classes, so similar classes can share code efficiently. You’ll store your classes in mod-ules and import classes written by other programmers into your own pro-gram files.\n"
13+
]
14+
},
15+
{
16+
"cell_type": "markdown",
17+
"metadata": {},
18+
"source": [
19+
"# Creating and Using a Class"
20+
]
21+
},
22+
{
23+
"cell_type": "markdown",
24+
"metadata": {},
25+
"source": [
26+
"## Creating a Class\n",
27+
"### Creating the Dog Class\n",
28+
"\n",
29+
"Each instance created from the Dog class will store a name and an age, and we’ll give each dog the ability to sit() and roll_over():\n",
30+
"\n",
31+
"1. Define a class *Dog*\n",
32+
"2. Write a docstring describing what this class does\n",
33+
" \n",
34+
"### The `__init__()` Method\n",
35+
"3. **`__init__`** is a special method that Python runs automatically whenever we create a new instance based on the Dog class\n",
36+
" 1. Has `two leading underscores and two trailing underscores`, **a convention that helps prevent Python's default method names of conflicting with your method names.**\n",
37+
" 1. If you do not use the underscores correctly, the method won't be called automatically when you use your class, which can result in errors that are difficult to identify.\n",
38+
"\n",
39+
"We define the `__init__()` method to have three parameters: `self`, `name`, and `age`. ***The self parameter is required in the method definition, and it must come first before the other parameters.*** It must be included in the definition because when Python calls this method later (to create an instance of Dog), the method call will automatically pass the self argument. *Every method call associated with an instance automatically passes self, which is a reference to the instance itself; it gives the individual instance access to the attributes and methods in the class.* When we make an instance of Dog, Python will call the `__init__()` method from the Dog class. ***We’ll pass `Dog()` a name and an age as arguments; self is passed automatically, so we don’t need to pass it.*** Whenever we want to make an instance from the Dog class, we’ll provide values for only the last two parameters, name and age.\n",
40+
"\n",
41+
"1. Two of the variables have the prefix `self`. ***Any variable prefixed with `self` is available to every method in the class, and we'll also be able to access these variables through any instance created from the class.***\n",
42+
" 1. The line `self.name = name` takes the value associated with the parameter `name` and assigns it to the variable `name`, which is then attached to the instance being created.\n",
43+
" 1. Variables that are accessible through instances like this are called `attributes`.\n",
44+
"2. The Dog class has two other methods defined: sit() and roll_over(). Because these methods don’t need additional information to run, we just define them to have one parameter, `self`. The instances we create later will have access to these methods. In other words, they’ll be able to sit and roll over."
45+
]
46+
},
47+
{
48+
"cell_type": "code",
49+
"execution_count": null,
50+
"metadata": {},
51+
"outputs": [],
52+
"source": [
53+
"class Dog:\n",
54+
" \"\"\"Aimple attempt to model a dog.\"\"\"\n",
55+
"\n",
56+
" def __init__(self, name, age):\n",
57+
" \"\"\"Initialize name and age attributes.\"\"\"\n",
58+
" self.name = name\n",
59+
" self.age = age\n",
60+
"\n",
61+
" def sit(self):\n",
62+
" \"\"\"Simulate a dog sitting in response to a command.\"\"\"\n",
63+
" print(f\"{self.name} is now sitting.\")\n",
64+
"\n",
65+
" def roll_over(self):\n",
66+
" \"\"\"Simulate rolling over in response to a command.\"\"\"\n",
67+
" print(f\"{self.name} rolled over!\")"
68+
]
69+
},
70+
{
71+
"cell_type": "markdown",
72+
"metadata": {},
73+
"source": []
74+
}
75+
],
76+
"metadata": {
77+
"language_info": {
78+
"name": "python"
79+
},
80+
"orig_nbformat": 4
81+
},
82+
"nbformat": 4,
83+
"nbformat_minor": 2
84+
}

0 commit comments

Comments
 (0)