You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/rst/python_oop.rst
+64-16
Original file line number
Diff line number
Diff line change
@@ -148,11 +148,11 @@ OOP is useful for the same reason that abstraction is useful: for recognizing an
148
148
149
149
For example,
150
150
151
-
* *a Markov chain* consists of a set of states and a collection of transition probabilities for moving across states
151
+
* *a Markov chain* consists of a set of states, an initial probability distribution over states, and a collection of transition probabilities for moving across states
152
152
153
153
* *a general equilibrium theory* consists of a commodity space, preferences, technologies, and an equilibrium definition
154
154
155
-
* *a game* consists of a list of players, lists of actions available to each player, player payoffs as functions of all players' actions, and a timing protocol
155
+
* *a game* consists of a list of players, lists of actions available to each player, each player's payoffs as functions of all other players' actions, and a timing protocol
156
156
157
157
These are all abstractions that collect together "objects" of the same "type".
158
158
@@ -179,20 +179,67 @@ Let's build some simple classes to start off.
179
179
.. _oop_consumer_class:
180
180
181
181
182
+
Before we do so, in order to indicate some of the power of Classes, we'll define two functions that we'll call ``earn`` and ``spend``.
183
+
184
+
.. code-block:: python3
185
+
186
+
def earn(w,y):
187
+
"Consumer with inital wealth w earns y"
188
+
return w+y
189
+
190
+
def spend(w,x):
191
+
"consumer with initial wealth w spends x"
192
+
new_wealth = w -x
193
+
if new_wealth < 0:
194
+
print("Insufficient funds")
195
+
else:
196
+
return new_wealth
197
+
198
+
The ``earn`` function takes a consumer's initial wealth :math:`w` and adds to it her current earnings :math:`y`.
199
+
200
+
The ``spend`` function takes a consumer's initial wealth :math:`w` and deducts from it her current spending :math:`x`.
201
+
202
+
We can use these two functions to keep track of a consumer's wealth as she earns and spends.
203
+
204
+
For example
205
+
206
+
.. code-block:: python3
207
+
208
+
w0=100
209
+
w1=earn(w0,10)
210
+
w2=spend(w1,20)
211
+
w3=earn(w2,10)
212
+
w4=spend(w3,20)
213
+
print("w0,w1,w2,w3,w4 = ", w0,w1,w2,w3,w4)
214
+
215
+
216
+
A Class bundles a set of data tied to a particular *instance* together with a collection of functions that operate on the data.
217
+
218
+
In our example, an *instance* will be the name of particular *person* whose *instance data* consist solely of its wealth.
219
+
220
+
(In other examples *instance data* will consist of a vector of data.)
221
+
222
+
In our example, two functions ``earn`` and ``spend`` can be applied to the current instance data.
223
+
224
+
Taken together, the instance data and functions are called *methods*.
225
+
226
+
These can be readily accessed in ways that we shall describe now.
227
+
228
+
182
229
Example: A Consumer Class
183
230
-------------------------
184
231
185
-
First, we'll build a ``Consumer`` class with
232
+
We'll build a ``Consumer`` class with
186
233
187
234
* a ``wealth`` attribute that stores the consumer's wealth (data)
188
235
189
236
* an ``earn`` method, where ``earn(y)`` increments the consumer's wealth by ``y``
190
237
191
238
* a ``spend`` method, where ``spend(x)`` either decreases wealth by ``x`` or returns an error if insufficient funds exist
192
239
193
-
Admittedly a little contrived, this example of a class helps us internalize some new syntax.
240
+
Admittedly a little contrived, this example of a class helps us internalize some peculiar syntax.
194
241
195
-
Here's one implementation
242
+
Here how we set up our Consumer class.
196
243
197
244
198
245
.. code-block:: python3
@@ -220,28 +267,28 @@ There's some special syntax here so let's step through carefully
220
267
221
268
* The ``class`` keyword indicates that we are building a class.
222
269
223
-
This class defines instance data ``wealth`` and three methods: ``__init__``, ``earn`` and ``spend``
270
+
The ``Consumer`` class defines instance data ``wealth`` and three methods: ``__init__``, ``earn`` and ``spend``
224
271
225
-
* ``wealth`` is *instance data* because each consumer we create (each instance of the ``Consumer`` class) will have its own separate wealth data.
272
+
* ``wealth`` is *instance data* because each consumer we create (each instance of the ``Consumer`` class) will have its own wealth data.
226
273
227
-
The ideas behind the ``earn`` and ``spend`` methods were discussed above.
274
+
The ``earn`` and ``spend`` methods deploy the functions we described earlier and that can potentially be applied to the ``wealth`` instance data.
228
275
229
-
Both of these act on the instance data ``wealth``.
230
276
231
277
The ``__init__`` method is a *constructor method*.
232
278
233
-
Whenever we create an instance of the class, this method will be called automatically.
279
+
Whenever we create an instance of the class, the ``__init_`` method will be called automatically.
234
280
235
281
Calling ``__init__`` sets up a "namespace" to hold the instance data --- more on this soon.
236
282
237
-
We'll also discuss the role of ``self`` just below.
283
+
We'll also discuss the role of the peculiar ``self`` bookkeeping device in detail below.
238
284
285
+
Usage
286
+
^^^^^^
239
287
240
-
Usage
241
-
^^^^^
242
288
289
+
Here's an example in which we use the class ``Consumer`` to crdate an instance of a consumer whom we affectionately name :math:`c1`.
243
290
244
-
Here's an example of usage
291
+
After we create consumer :math:`c1` and endow it with initial wealth :math:`10`, we'll apply the ``spend`` method.
245
292
246
293
.. code-block:: python3
247
294
@@ -283,12 +330,13 @@ When we access or set attributes we're actually just modifying the dictionary
283
330
maintained by the instance.
284
331
285
332
Self
286
-
^^^^
333
+
^^^^^
334
+
287
335
288
336
If you look at the ``Consumer`` class definition again you'll see the word
289
337
`self` throughout the code.
290
338
291
-
The rules with ``self`` are that
339
+
The rules for using ``self`` in creating a Class are that
292
340
293
341
* Any instance data should be prepended with ``self``
0 commit comments