Syllabus Lesson 19 of 246 · Module 1: Python for this course
Module 1: Python for this course

Objects that remember (classes and closures)

Why it matters In Module 4 you will build an autograd engine. It is a class whose objects add and multiply with ordinary + and @, and each result keeps a small function that remembers how it was made. This lesson is the Python behind that.

A class is a recipe for objects

class Box:
    def __init__(self, value):      # runs when you write Box(3)
        self.value = value          # self is the object being made

    def doubled(self):              # a method: a function that belongs to the object
        return Box(self.value * 2)


b = Box(3)
b.value          # 3
b.doubled().value   # 6

Making + and @ work

When Python sees a + b on your objects, it calls a.__add__(b). For a @ b it calls a.__matmul__(b). Write those methods and your objects work with the normal symbols. The names with two underscores on each side are called dunder methods.

    def __add__(self, other):
        return Box(self.value + other.value)

A value that is computed when you ask (@property)

Put @property above a method and you read it like a stored value, without brackets: b.size instead of b.size(). It is worked out fresh each time.

Small functions: lambda

square = lambda x: x * x is a one-line function with no name of its own. It is the same as def square(x): return x * x.

Functions that remember (closures)

def make_scaler(k):
    def scale(x):
        return k * x       # k comes from the call to make_scaler
    return scale


triple = make_scaler(3)
triple(5)        # 15: scale still remembers k = 3

Reading a remembered variable just works. Changing one needs care: count += 1 inside the inner function makes Python treat count as a brand-new local variable, and you get UnboundLocalError. Two fixes: write nonlocal count as the inner function's first line, which says "the count from outside", or keep the number inside a list, count = [0], and change count[0], which edits the list instead of rebinding the name.

def make_total():
    total = 0
    def add(x):
        nonlocal total     # without this line: UnboundLocalError
        total += x
        return total
    return add

The inner function keeps the variables it used from the function that made it, even after that function has finished. Autograd uses exactly this: each result of + or * stores a tiny closure that remembers its inputs, ready to send gradients back to them later.

Class: a recipe for objects. Method: a function that belongs to an object and receives it as self. Closure: an inner function that remembers variables from where it was made.

Recap __init__ sets an object up, __add__ and __matmul__ give it + and @, @property reads like a value, and a closure carries its own memory.

Words and symbols in this lesson
a @ b
matrix multiply (also the dot product for two lists of numbers)
Your turn

Finish the class Vec, a list of numbers that does maths. Vec([1, 2]) + Vec([3, 4]) must give a new Vec holding [4, 6] (add item by item); Vec([1, 2]) @ Vec([3, 4]) must give the number 11 (multiply item by item and add up); Vec([3, 4]).size must be 5.0 (the length, the square root of the sum of squares), read as a property. Neither input may change. Then write make_counter(): it returns a function that, each time it is called, returns 1, then 2, then 3 and so on, with each counter counting on its own.

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.