Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Feb 4, 2026. It is now read-only.

Commit cc15724

Browse filesBrowse files
committed
Edits
1 parent 7a1cccb commit cc15724
Copy full SHA for cc15724

File tree

Expand file treeCollapse file tree

7 files changed

+131
-114
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

7 files changed

+131
-114
lines changed
Open diff view settings
Collapse file

‎Notes/01_Introduction/02_Hello_world.md‎

Copy file name to clipboardExpand all lines: Notes/01_Introduction/02_Hello_world.md
-5Lines changed: 0 additions & 5 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -291,24 +291,19 @@ The `if` statement is used to execute a conditional:
291291

292292
```python
293293
if a > b:
294-
# a is greater than b
295294
print('Computer says no')
296295
else:
297-
# a is lower or equal to b
298296
print('Computer says yes')
299297
```
300298

301299
You can check for multiple conditions by adding extra checks using `elif`.
302300

303301
```python
304302
if a > b:
305-
# a is greater than b
306303
print('Computer says no')
307304
elif a == b:
308-
# a is equal to b
309305
print('Computer says yes')
310306
else:
311-
# a is lower to b
312307
print('Computer says maybe')
313308
```
314309

Collapse file

‎Notes/01_Introduction/06_Files.md‎

Copy file name to clipboardExpand all lines: Notes/01_Introduction/06_Files.md
+1-1Lines changed: 1 addition & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ with open(filename, 'rt') as file:
6666
# Process the line
6767
```
6868

69-
### Common Idioms for Write to a File
69+
### Common Idioms for Writing to a File
7070

7171
Write string data.
7272

Collapse file

‎Notes/02_Working_with_data/07_Objects.md‎

Copy file name to clipboardExpand all lines: Notes/02_Working_with_data/07_Objects.md
+1-1Lines changed: 1 addition & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ True
139139
>>>
140140
```
141141

142-
For example, the inner list `[100, 101]` is being shared.
142+
For example, the inner list `[100, 101, 102]` is being shared.
143143
This is known as a shallow copy. Here is a picture.
144144

145145
![Shallow copy](shallow.png)
Collapse file

‎Notes/04_Classes_objects/01_Class.md‎

Copy file name to clipboardExpand all lines: Notes/04_Classes_objects/01_Class.md
+33-24Lines changed: 33 additions & 24 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1+
[Contents](../Contents) \| [Previous (3.6 Design discussion)](../03_Program_organization/06_Design_discussion) \| [Next (4.2 Inheritance)](02_Inheritance)
2+
13
# 4.1 Classes
24

5+
This section introduces the class statement and the idea of creating new objects.
6+
37
### Object Oriented (OO) programming
48

5-
A Programming technique where code is organized as a collection of *objects*.
9+
A Programming technique where code is organized as a collection of
10+
*objects*.
611

712
An *object* consists of:
813

914
* Data. Attributes
10-
* Behavior. Methods, functions applied to the object.
15+
* Behavior. Methods which are functions applied to the object.
1116

1217
You have already been using some OO during this course.
1318

14-
For example with Lists.
19+
For example, manipulating a list.
1520

1621
```python
1722
>>> nums = [1, 2, 3]
@@ -24,14 +29,14 @@ For example with Lists.
2429

2530
`nums` is an *instance* of a list.
2631

27-
Methods (`append` and `insert`) are attached to the instance (`nums`).
32+
Methods (`append()` and `insert()`) are attached to the instance (`nums`).
2833

2934
### The `class` statement
3035

3136
Use the `class` statement to define a new object.
3237

3338
```python
34-
class Player(object):
39+
class Player:
3540
def __init__(self, x, y):
3641
self.x = x
3742
self.y = y
@@ -59,9 +64,10 @@ They are created by calling the class as a function.
5964
>>>
6065
```
6166

62-
`a` anb `b` are instances of `Player`.
67+
`a` and `b` are instances of `Player`.
6368

64-
*Emphasize: The class statement is just the definition (it does nothing by itself). Similar to a function definition.*
69+
*Emphasize: The class statement is just the definition (it does
70+
nothing by itself). Similar to a function definition.*
6571

6672
### Instance Data
6773

@@ -77,7 +83,7 @@ Each instance has its own local data.
7783
This data is initialized by the `__init__()`.
7884

7985
```python
80-
class Player(object):
86+
class Player:
8187
def __init__(self, x, y):
8288
# Any value stored on `self` is instance data
8389
self.x = x
@@ -92,7 +98,7 @@ There are no restrictions on the total number or type of attributes stored.
9298
Instance methods are functions applied to instances of an object.
9399

94100
```python
95-
class Player(object):
101+
class Player:
96102
...
97103
# `move` is a method
98104
def move(self, dx, dy):
@@ -113,15 +119,15 @@ def move(self, dx, dy):
113119

114120
By convention, the instance is called `self`. However, the actual name
115121
used is unimportant. The object is always passed as the first
116-
argument. It is simply Python programming style to call this argument
122+
argument. It is merely Python programming style to call this argument
117123
`self`.
118124

119125
### Class Scoping
120126

121-
Classes do not define a scope.
127+
Classes do not define a scope of names.
122128

123129
```python
124-
class Player(object):
130+
class Player:
125131
...
126132
def move(self, dx, dy):
127133
self.x += dx
@@ -132,13 +138,15 @@ class Player(object):
132138
self.move(-amt, 0) # YES. Calls method `move` from above.
133139
```
134140

135-
If you want to operate on an instance, you always have to refer too it explicitly (e.g., `self`).
141+
If you want to operate on an instance, you always refer to it explicitly (e.g., `self`).
136142

137143
## Exercises
138144

139-
Note: For this exercise you want to have fully working code from earlier
140-
exercises. If things are broken look at the solution code for Exercise 3.18.
141-
You can find this code in the `Solutions/3_18` directory.
145+
Starting with this set of exercises, we start to make a series of
146+
changes to existing code from previous sctions. It is critical that
147+
you have a working version of Exercise 3.18 to start. If you don't
148+
have that, please work from the solution code found in the
149+
`Solutions/3_18` directory. It's fine to copy it.
142150

143151
### Exercise 4.1: Objects as Data Structures
144152

@@ -206,8 +214,8 @@ Create a few more `Stock` objects and manipulate them. For example:
206214

207215
One thing to emphasize here is that the class `Stock` acts like a
208216
factory for creating instances of objects. Basically, you call
209-
it as a function and it creates a new object for you. Also, it needs
210-
to be emphasized that each object is distinct---they each have their
217+
it as a function and it creates a new object for you. Also, it must
218+
be emphasized that each object is distinct---they each have their
211219
own data that is separate from other objects that have been created.
212220

213221
An object defined by a class is somewhat similar to a dictionary--just
@@ -238,8 +246,8 @@ stored inside an object. Add a `cost()` and `sell()` method to your
238246

239247
### Exercise 4.3: Creating a list of instances
240248

241-
Try these steps to make a list of Stock instances and compute the total
242-
cost:
249+
Try these steps to make a list of Stock instances from a list of
250+
dictionaries. Then compute the total cost:
243251

244252
```python
245253
>>> import fileparse
@@ -258,10 +266,11 @@ cost:
258266

259267
### Exercise 4.4: Using your class
260268

261-
Modify the `read_portfolio()` function in the `report.py` program so that it
262-
reads a portfolio into a list of `Stock` instances. Once you have done that,
263-
fix all of the code in `report.py` and `pcost.py` so that it works with
264-
`Stock` instances instead of dictionaries.
269+
Modify the `read_portfolio()` function in the `report.py` program so
270+
that it reads a portfolio into a list of `Stock` instances as just
271+
shown in Exercise 4.3. Once you have done that, fix all of the code
272+
in `report.py` and `pcost.py` so that it works with `Stock` instances
273+
instead of dictionaries.
265274

266275
Hint: You should not have to make major changes to the code. You will mainly
267276
be changing dictionary access such as `s['shares']` into `s.shares`.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.