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
712An * 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
1217You 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
3136Use 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.
7783This 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.
9298Instance 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
114120By convention, the instance is called ` self ` . However, the actual name
115121used 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
207215One thing to emphasize here is that the class ` Stock ` acts like a
208216factory 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
211219own data that is separate from other objects that have been created.
212220
213221An 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
266275Hint: You should not have to make major changes to the code. You will mainly
267276be changing dictionary access such as `s[' shares' ]` into `s.shares` .
0 commit comments