Topic 5: Classes
Homework Questions
Python Programming for Economics
Dr. Marcus Roel
Spring 2024, UNNC
Standard Homework
go over our examples from the lecture and (1) understand them, and (2) vary them
slightly to see if you really understand them.
Classes
Experiment with classes by creating any object you find interesting. Afterwards,
create a parent class.
potential examples: a person, a student; a motor-vehicle, a car, a bus; a video
game weapon, a video game sword (or something less violent); an economic
agent, a consumer (solving your typical micro 1/2 problems); animals; flowers;
etc : )
think about potential things (verbs) these objects should be able to do.
Do experiment with classes by creating any object you find interesting
If you haven't, try to search your particular example online.
Unless you were very creative, you can surely find it.
Playing with Rectangles
Create a class rectangle with a method that allows the rectangle to be cut in half
(along the largest side)
next level version: have the class return the cut-away part of the rectangle as an
object iself
next next level: write a function (using the above class) to calculate the sum of
the area of the following objects: (the area of) an initial square, (the area of) the
initial square cut in half, (the area) of the cut-in-half square cut in half, ... up to
n-cuts!
Class Attributes
Create a class with a class attribute. Create an instance (a) of a class and check the
value of said attribute. Create another instance (b) of the class, and change the
class attribute for this instance. Check the value of the class attribute again for
instance a.
Staticmethod
Write a class with a staticmethod. Next write the same staticmethod also a normal
method - passing in the self tag and never use it inside.
Try calling both methods after instantializing a version of the class. Try also
calling both methods without creating an instance.
Inheritance
In the class square of our lecture notes, we called
super().__init__(length, length) in the __init__ method. How could
you write the class without relying on the parent's initialization? (If you get it right on
the first try, also try to write a version that doesn't work).
In [ ]: