4. Object Oriented Programming#
This chapter was coauthored by Jason DeBacker and Richard W. Evans.
Python is literally a programming language built on objects. Objects are instances of classes. And classes are definitions of objects with their corresponding methods and attributes. Objects are a powerful way to group functionality and attributes in a class that has a limited and common set of characteristics.
An analogy is how life forms are classified by taxonomic rank going from most general to most specific: domain, kingdom, phylum, class, order, family, genus, and species [Wikipedia Contributors, 2023]. If you have a model of all the different types of cats, you would probably care about the taxonomic rank family of felidae or cats. If you were interested in modeling all the different types of mammals that live on land, you might need many different orders, with sub-class objects for each family, genus, and species within each order.
Python objects defined as classes have a limited set of attributes that apply to that class in the same way the cat family felidae has different attributes than the dog family canidae. In the family of OG-Core
macroeconomic model country calibrations, we have many custom objects defined by classes, the most important of which might be the parameters
class.
Using objects wisely and efficiently can make your code more readable, easier to modify and use, more scalable, and more interoperable. The iframe below contains a PDF of the BYU ACME open-access lab entitled, “Object-oriented Programming”. You can either scroll through the lab on this page using the iframe window, or you can download the PDF for use on your computer. See [BYU ACME, 2023]. Exercise 4.1 below has you work through the problems in this BYU ACME lab. A Python file (object_oriented.py
) template for the problems in this lab is stored in the ./code/ObjectOriented/
directory.
4.1. Exercises#
Read the BYU ACME “Object-oriented programming” lab and complete Problems 1 through 4 in the lab. [BYU ACME, 2023]
Define a class called Specifications
with an attribute that is the rate of time preference beta
(usually represented by the Greek letter \(\beta\)). Create two instances of this class, the first called p1
for beta=0.96
and the second called p2
for beta=0.99
.
Update the Specifications
class from Exercise 4.2 so that it not only allows one to specify the value of beta
upon instantiation of the class but also checks that beta
is between 0 and 1.
Modify the Specifications
class from Exercise 4.3 so that it has a method that prints the value of beta
.
Building off the Specifications
class in Exercise 4.4, change the input of beta
to the class so that it is input at an annual rate beta_annual
. Allow another attribute of the class called S
that is the number of periods in an economic agent’s life. Include a method in the Specifications
class that adjusts the value of beta
to represent the discount rate applied per model period. Let each model period be S/80
years, such that each model period equals one years when S=80
.
Add a method to the Specifications
class in Exercise 4.5 that allows one to update the values of the class attributes S
and beta_annual
by providing a dictionary of the form {"S": 40, "beta_annual": 0.8}
. Ensure that when the instance is updated, the new beta
attribute is consistent with the new S
and beta_annual
.