Lecture 26: Classes & Memory
August 4th, 2021
Today: More about classes and memory
Announcements
- Quiz 2 is on Monday, August 9th starting at 1:30pm PT
- Review materials posted on the website
- Slides are going to look a bit different today because this topic is best taught with images
What does it take to write big scale software?
- What does it take to go from what you know now to writing big scale software?
- There are some large programs written in Python
-Spotify
-Youtube
-Dropbox
-Reddit
- Let's talk about Spotify. How did they write that code?
-Defined new variable types
-Song, Playlist, User, Song Player, Song Retriever
- You have been using variable types all quarter
-Bit, Simple Image, Grid, Canvas, String, int
- What would it take to define your own?
Classes and Objects
- Classes define new variable types
- Classes decompose your program across files
- Classes are blueprints like bluprints
-A template for a new variable type
- In a class you need to define 3 things:
1. Instance Varibales
-what sub-variables does each instance store?
2. Instance Methods
-what methods can you call on an instance?
3. Constructor
-what happens when you make a new one?
Classes Review
dog.py
class Dog:
# constructor
def __init__(self):
self.times_barked = 0 # instance variable
# instance method
def bark(self):
print('woof')
self.times_barked += 1
Let's break down the above class
1. Instance Variables - what sub-variables does each instance store?
self.times_barked
2. Instance Methods - what methods can you call on an instance?
# instance method
def bark(self):
print('woof')
self.times_barked += 1
3. Constructor - what happens when you make a new one?
# constructor
def __init__(self):
self.times_barked = 0 # instance variable
Let's review how we use classes
life.py
def main():
simba = Dog()
juno = Dog()
simba.bark()
juno.bark()
simba.bark()
print(simba.__dict__)
print(juno.__dict__)
What is simba.__dict__
?
- Did I mention that a class is like a fancy dictionary?
- Each object has a special property (
__dict__
) that shows all instance variables and their values for that object
- print(simba.__dict__)
- {'times_barked': 2}
What is self
?
- When authoring a class, self means:
"the instance (aka object) that I am currenlty working with"
- Recall that the parameter
self
is automatically set by Python as the object that this method is being called on
-Ex: you write simba.bark()
-Python treats it as bark(simba)
Classes Split Up Work Just Like Functions
- Recall functions:
-function author (writes helper functions others can use)
-function caller (uses helper functions)
- Classes also split up the work
-Class author (writes the class, thus defining a new variable type)
-Class client (uses the new variable types to solve problems - often from main())
Next step in writing larger programs: Better Understand Memory
What does this do?
Slides borrowed from Piech + Sahami 2020
The Stack
- Each time a function is called, a new frame of memory is created
- Each frame has space for all of the local variables declared in the function, and parameters
- Each variable has a reference, which is like a URL
- When a function returns, its stack frame is destroyed
The Heap
- Where values are stored
- Every value has an address (like a URL address)
- Values do not go away when functions return
- Memory is recycled when it is not longer being used (ref count = 0)
Let's take a closer look: x = x + 1
- x = x + 1
- x = x + 1
-when a variable is "assigned", you are changing its reference
-you know if a variable is being assigned to if it is on the left side of an = sign
- x = x + 1
-when a variable is "used" you are accessing its value
Memory Example with Mutiple Functions
Let's checkout how Classes & Objects use memory
Slides borrowed from Piech + Sahami 2020
Challenge for You