[We hope that you can view the videos! However, if your internet connection is too slow for viewing videos, download the audio file (mp4) and the slides (pdf). Then you can listen to the audio as you go through the slides.]

Classes

We can create our own custom type (as opposed to Python's built-in types) by defining classes. Here are the slides for both videos of this lecture: pdf. The answers to the questions asked during this lecture are in a separate file at the end of this page.

Part 1: Initializing attributes

In this segment we start a simple class definition that has two parts: the specifications and the __init__ method. We define the __init__ method, which is called by the constructor in order to assign attributes (variables in an object of a class). We also talk about the class specifications--class invariant--and a way to enforce the invariant by using assert statements. We end this segment by showing you how to make arguments optional.

Part 2: Class attributes and method definitions

A variable that belongs to the class and is shared by all the instances is a Class Attribute. Be careful to use to class name, not an object variable name, to access a class attribute. We show you how to define methods in the class: (1) have self as the first parameter in any method's parameter list and (2) use self to access attributes in the method body.

Answers to the 2 questions asked during lecture: pdf