Lecture 36 - Under the Hood

Lecture Date: Wednesday, November 18

Did you ever wonder exactly where all this stuff we’ve been talking was being stored on your computer?

There are different types of “memory” in a computer

  • Cache - small, really fast memory that’s attached to the processor - used as “working memory”
  • RAM (Random Access Memory) - holds info about programs currently running - used as “short term memory”
  • Hard disk / SSD - holds data that will persist if the power is cut off - used as “long term memory”

Your programs are .py files, stored on the disk itself. But when they run, the Python interpreter requests from the operating system that some space be set aside for your program to run in. This is typically in RAM (although it can vary a little).

Once the program gets going, you have something called the stack and the heap.


Image credit: thenewcircle.com

The stack is where variable pointers go, such as with a complex/mutable type. The actual values of the complex/mutable type variable is found in the heap.

What happens when a variable no longer points to something in the heap? This is called garbage collection.

Let’s look at some examples at this tutorial: http://www.pythontutor.com/

How exactly does Python keep up with what type a variable is? How do other languages do it?

The War between Filing Systems

There are lots of numbers in your life. Social security numbers, student ID numbers, SIS person numbers, telephone numbers, zip codes, … How do you know which number is which?

Option 1: remember. This number is on the post-it beside my bed, which is where I wrote my student ID number, so that must be what it is.

Option 2: annotate. This piece of paper says SSN: 012345678 so it must by my SSN, not my telephone number.

In programming we call the first static typing and the latter dynamic typing. People argue about which one is better heatedly.

Python happens to be dynamically typed. Java, on the other hand, is statically typed.

How does this affect how we write our programs?

Imagine a program that you might write that has these lines:

x = 4
x = "Hello World"
x = gamebox.Camera()

In Python, this works just fine, because of the dynamic typing. But in Java, this would never work, because we would have defined x to have a particular type. After that, it can’t be changed. But, if we store the type with the data itself and not the variable, then this works just fine. x is just a pointer to some data - how we interpret it is up to the programmer.

You might think that dynamic typing can get a programmer in a lot of trouble if they don’t keep their variables straight. And you’re right. But it does make for less coding in general, even if dynamically typed languages are usually slower than statically typed ones.

Java is also a strongly typed language, which means that once you set a type, it can never be changes. Oh sure, you can cast it, but that’s just the type being temporarily converted. The original data stays with the original type.

So, if we’re building a program, and given a choice, which would you prefer to do?

  • Write a program quickly
  • Write a program that runs fast
  • Write a program that has few if any bugs
  • Write a program that you can read and make sense of latter
  • Write a program that runs on many different devices

This partially drives our choice of language.