Skip to content

Commit b9e5831

Browse files
Tom's edits of one lecture
1 parent 49abeaf commit b9e5831

File tree

1 file changed

+64
-16
lines changed

1 file changed

+64
-16
lines changed

source/rst/python_oop.rst

+64-16
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ OOP is useful for the same reason that abstraction is useful: for recognizing an
148148

149149
For example,
150150

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
152152

153153
* *a general equilibrium theory* consists of a commodity space, preferences, technologies, and an equilibrium definition
154154

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
156156

157157
These are all abstractions that collect together "objects" of the same "type".
158158

@@ -179,20 +179,67 @@ Let's build some simple classes to start off.
179179
.. _oop_consumer_class:
180180

181181

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+
182229
Example: A Consumer Class
183230
-------------------------
184231

185-
First, we'll build a ``Consumer`` class with
232+
We'll build a ``Consumer`` class with
186233

187234
* a ``wealth`` attribute that stores the consumer's wealth (data)
188235

189236
* an ``earn`` method, where ``earn(y)`` increments the consumer's wealth by ``y``
190237

191238
* a ``spend`` method, where ``spend(x)`` either decreases wealth by ``x`` or returns an error if insufficient funds exist
192239

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.
194241

195-
Here's one implementation
242+
Here how we set up our Consumer class.
196243

197244

198245
.. code-block:: python3
@@ -220,28 +267,28 @@ There's some special syntax here so let's step through carefully
220267

221268
* The ``class`` keyword indicates that we are building a class.
222269

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``
224271

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.
226273

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.
228275

229-
Both of these act on the instance data ``wealth``.
230276

231277
The ``__init__`` method is a *constructor method*.
232278

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.
234280

235281
Calling ``__init__`` sets up a "namespace" to hold the instance data --- more on this soon.
236282

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.
238284

285+
Usage
286+
^^^^^^
239287

240-
Usage
241-
^^^^^
242288

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`.
243290

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.
245292

246293
.. code-block:: python3
247294
@@ -283,12 +330,13 @@ When we access or set attributes we're actually just modifying the dictionary
283330
maintained by the instance.
284331

285332
Self
286-
^^^^
333+
^^^^^
334+
287335

288336
If you look at the ``Consumer`` class definition again you'll see the word
289337
`self` throughout the code.
290338

291-
The rules with ``self`` are that
339+
The rules for using ``self`` in creating a Class are that
292340

293341
* Any instance data should be prepended with ``self``
294342

0 commit comments

Comments
 (0)