1
+ [ Contents] ( ../Contents ) \| [ Previous (3.6 Design discussion)] ( ../03_Program_organization/06_Design_discussion ) \| [ Next (4.2 Inheritance)] ( 02_Inheritance )
2
+
1
3
# 4.1 Classes
2
4
5
+ This section introduces the class statement and the idea of creating new objects.
6
+
3
7
### Object Oriented (OO) programming
4
8
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* .
6
11
7
12
An * object* consists of:
8
13
9
14
* Data. Attributes
10
- * Behavior. Methods, functions applied to the object.
15
+ * Behavior. Methods which are functions applied to the object.
11
16
12
17
You have already been using some OO during this course.
13
18
14
- For example with Lists .
19
+ For example, manipulating a list .
15
20
16
21
``` python
17
22
>> > nums = [1 , 2 , 3 ]
@@ -24,14 +29,14 @@ For example with Lists.
24
29
25
30
` nums ` is an * instance* of a list.
26
31
27
- Methods (` append ` and ` insert ` ) are attached to the instance (` nums ` ).
32
+ Methods (` append() ` and ` insert() ` ) are attached to the instance (` nums ` ).
28
33
29
34
### The ` class ` statement
30
35
31
36
Use the ` class ` statement to define a new object.
32
37
33
38
``` python
34
- class Player ( object ) :
39
+ class Player :
35
40
def __init__ (self , x , y ):
36
41
self .x = x
37
42
self .y = y
@@ -59,9 +64,10 @@ They are created by calling the class as a function.
59
64
>> >
60
65
```
61
66
62
- ` a ` anb ` b ` are instances of ` Player ` .
67
+ ` a ` and ` b ` are instances of ` Player ` .
63
68
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.*
65
71
66
72
### Instance Data
67
73
@@ -77,7 +83,7 @@ Each instance has its own local data.
77
83
This data is initialized by the ` __init__() ` .
78
84
79
85
``` python
80
- class Player ( object ) :
86
+ class Player :
81
87
def __init__ (self , x , y ):
82
88
# Any value stored on `self` is instance data
83
89
self .x = x
@@ -92,7 +98,7 @@ There are no restrictions on the total number or type of attributes stored.
92
98
Instance methods are functions applied to instances of an object.
93
99
94
100
``` python
95
- class Player ( object ) :
101
+ class Player :
96
102
...
97
103
# `move` is a method
98
104
def move (self , dx , dy ):
@@ -113,15 +119,15 @@ def move(self, dx, dy):
113
119
114
120
By convention, the instance is called ` self ` . However, the actual name
115
121
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
117
123
` self ` .
118
124
119
125
### Class Scoping
120
126
121
- Classes do not define a scope.
127
+ Classes do not define a scope of names .
122
128
123
129
``` python
124
- class Player ( object ) :
130
+ class Player :
125
131
...
126
132
def move (self , dx , dy ):
127
133
self .x += dx
@@ -132,13 +138,15 @@ class Player(object):
132
138
self .move(- amt, 0 ) # YES. Calls method `move` from above.
133
139
```
134
140
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 ` ).
136
142
137
143
## Exercises
138
144
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.
142
150
143
151
### Exercise 4.1: Objects as Data Structures
144
152
@@ -206,8 +214,8 @@ Create a few more `Stock` objects and manipulate them. For example:
206
214
207
215
One thing to emphasize here is that the class ` Stock ` acts like a
208
216
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
211
219
own data that is separate from other objects that have been created.
212
220
213
221
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
238
246
239
247
### Exercise 4.3: Creating a list of instances
240
248
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:
243
251
244
252
``` python
245
253
>> > import fileparse
@@ -258,10 +266,11 @@ cost:
258
266
259
267
# ## Exercise 4.4: Using your class
260
268
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.
265
274
266
275
Hint: You should not have to make major changes to the code. You will mainly
267
276
be changing dictionary access such as `s[' shares' ]` into `s.shares` .
0 commit comments