Exploring Economics Summer Acadamy
Contents of this lecture:
As you have have noticed, everything in python is an object.
All objects are instances of a particular
For example, '2' is an instance of the class
int (i.e. integer):
The relates to the
object-oriented nature of Python (more on this in the lab notes).
Every object in python is an instance (or "realization") of a class.
The class serves as a "blueprint" for its instances.
A class comes with its own methods, i.e. functions that work on the instances of the class.
Consider a list as an example:
l = [1, 2, 3 , 4] # the list "[1, 2, 3, 4]" gets associated with the variable l print(type(l)) # returns: <class 'list'>
Meaning: there is a python class called "list" and l is an instance of this list.
The class list has some methods associated with it.
For example, every instance of the class list, has the method
l.sort() # as a method, sort takes itself as the first input l # Note that the method has changed the list!
[1, 2, 3, 4]
The concept of classes is super useful, because it allows you to create a blueprint for things that all have a similar structe.
For example, you could define a class
game, which summarizes all the aspects typically associated with a game:
When we later move to ABM, one important class will be the agents: they will have different attributes and methods defined on them.
Just as in the following example:
class Agent(): """ Here comes the docstring. This class represents a simple economic agents. She has two attributes, wealth and skill, and features three methods methods: one to get income, the other to consume income, the third to invest into education. """ def __init__(init_wealth): """ This function is called whenever we create an instance of the class. """ self.wealth = init_wealth self.skill = 0.0 # All new Agents start with zero skill def earn_income(earned_income): self.wealth += earned_income def consume(consumption): if self.wealth >= consumption: self.wealth -= consumption else: print("Not enough wealth: ", self.wealth) pass def educate(education_costs): if self.wealth >= education_costs: self.wealth -= education_costs self.skill += 0.5*education_costs else: print("Not enough wealth: ", self.wealth) pass
All classes are built according to this scheme.
We will now move to the analysis of networks, and illustrate some concepts of object-oriented programming and classes on the fly.
You might have guessed correctly that one of the most important classes we will deal with, are graphs.
But before we move on, build a class
Network on your own. Keep in mind, the class should be a blueprint from which you can construct typical networks quite easily.
class Network: """ This is a network class. It takes an edgelist for its construction. It features methods that return the nb of nodes or edges, or that return its degree distribution. """ def __init__(self, edge_list): self.edge_list = edge_list self.node_list = list(range(max(max(self.edge_list)))) def nb_nodes(self): return(len(self.node_list)) def nb_edges(self): return(len(self.edge_list)) test_edge_list = [(1,2),(2,4), (0,3)] test_network = Network(test_edge_list) test_network.nb_edges()