Python + oTree Crash Course

Session 3 - Python CLasses

Ali Seyhun Saral (Institute for Advanced Study in Toulouse)

IMPRS Be Smart Summer School

2023-08-07

Classes and Object-Oriented Programming

Procedural Programming

  • R scripts are a series of commands that are executed in order

  • For instane R or Stata (and what we have done so far)

Object-Oriented Programming

  • Objects are independent units that have some variables and functions attached to them.

  • Object: state and behavior bundled together

Table-Based Workflow

Name Awards Partner
Art 10 Paul
Cher 20 Sonny
Paul 20 Art
Sonny 5 Cher

Table-Based Workflow

Name Awards Partner
Art 10 Paul
Cher 20 Sonny
Paul 20 Art
Sonny 5 Cher
def addAwards(name):
  table[name, ‘Awards’] += 1
addAwards("Cher")


def make_partner(name1, name2):
  table[name1, ‘Partner’] += name2
  table[name2, ‘Partner’] += name1
}
make_partner("Art", "Paul")

Object-Oriented Programming

cher.add_award()

art.make_partner(paul)
cher.awards

## output: 10
art.partner   # paul
paul.partner # art

art.partner.partner # art
art.partner.add_award() # paul gets an award

Object-Oriented Programming

  • Objects have some variables attribute (art.award, art.partner)

  • Objects have some methods functions attached to them method (art.add_award())

  • Object are independent units.

Object-Oriented Programming

Classes

  • Classes are blueprints in which an object is generated.

  • An object created wrom a class is called an instance

  • Usually a class ansers four questions:

    • What are the variables related to it? (attributes)
    • What are the functions related to it? (methods)
    • What to do when I first create an object? (__init__)
    • Is there another class I should inherit from? (parent class)

Classes

Classes

Class Inheritance

Classes

Object-Orented Programming Properties

  1. Encapsulation: Self-contained pieces

  2. Abstractation: What is going on underneath is abstracted away

  3. Inheritance: Classes can be organized hierarchially

  4. Polymorphism: Classes can change into different forms from the parents

How to create Classes in Python

class Singer:
  def __init__(self):
    self.awards = 0

  def sing(self):
    print("la la la laaa!")

  def win_award(self):
    self.award = self.award + 1
  • Two unfamiliar things:
    • __init__: basically a function run automatically when an instance is created
    • self: can be read myself. If refers to the instance that is created from it.

Creating an insance

class Singer:
  def __init__(self):
    self.awards = 0

  def sing(self):
    print("la la la laaa!")

  def win_award(self):
    self.award = self.award + 1
ali = Singer()

ali.sing()
la la la laaa!

Practice time

07_classes.py

Class Inheritance

When you create a class with inheritance, put the name of the parent class inside the parantheses.

class Singer:
  def __init__(self):
    self.awards = 0

  def sing(self):
    print("la la la laaa!")

  def win_award(self):
    self.award = self.award + 1


class OperaSinger(Singer):
    def sing_aria(self):
      print("Ridi, Pagliaccio... sul tuo amore infranto!")

Class Inheritance

angelo = OperaSinger()

angelo.sing()
la la la laaa!


angelo = OperaSinger()

angelo.sing_aria()
Ridi, Pagliaccio... sul tuo amore infranto!

Class Inheritance

  • If you need to reach the parent class from a child class, you can call super() function.

  • If you define a method with the same name in the child class, the method of the parent will be overwritten.

  • If this is not the behavior you want, you can call the super function and extend the method over it.

Class Inheritance: super()

class Singer:
  def __init__(self):
    self.awards = 0

  def sing(self):
    print("la la la laaa!")

  def win_award(self):
    self.award = self.award + 1


class OperaSinger(Singer):
    def __init__(self):
      self.vocal_range = "tenor"

    def sing_aria(self):
      print("Ridi, Pagliaccio... sul tuo amore infranto!")


angelo = OperaSinger()

angelo.awards
AttributeError: 'OperaSinger' object has no attribute 'awards'

Class Inheritance: super()

class Singer:
  def __init__(self):
    self.awards = 0

  def sing(self):
    print("la la la laaa!")

  def win_award(self):
    self.award = self.award + 1
class OperaSinger(Singer):
    def __init__(self):
      super().__init__()
      self.vocal_range = "tenor"

    def sing_aria(self):
      print("Ridi, Pagliaccio... sul tuo amore infranto!")


angelo = OperaSinger()

angelo.awards
0