We can create our own custom type (as opposed to Python's built-in types) by defining classes . In a class definition, we specify what an instance of a class should have as data--the object attributes --and what an instance can do-- methods . We can also create variables to store values that are shared by all instances of a class-- class attributes .

Lecture Prep

Watch:
Note that we do not use the module introcs referenced in the lessons. The classes Point3, RGB , and CMYK from introcs are mentioned in these videos as examples of classes but you do not have to have seen them in order to understand the topics introduced. We have seen different examples this semester.
  1. Class Visualization (7 minutes) Python generates a folder to store class information--take a look with both Python Tutor and our own diagrams.
  2. Classes and Attributes (8 minutes) We can have object attributes for data stored in individual instances of a class. We also can have class attributes that store data shared by all instances of a class.
  3. Method Definitions (8 minutes) A method is a function that is defined inside a class definition. A method is to work with instances of the class.
  4. Method Visualization (7 minutes) Let's visualize a method call and see how it differs from a function call. There is a typo in the name of the call frame shown from 1:24 to 3:38 of the video: the frame should be labelled Point.distance , not Point3.distance (because the class name is Point and not Point3 ).

Or Read: Chapter 15 , 17.1 - 17.5

Syntax note: The class header can be written in different ways in Python 3.x. For a simple class, we, and the author of our textbook, write the class header as class classname : . For example, if we define a class Point , then the class definition would begin with this line of code: class Point: If you look at Python code from other sources, however, you may see class Point(): or class Point(object): as the class header. These three ways of writing the class header are equivalent in Python 3 (but not in Python 2). We will explain the details of this syntax in a couple of weeks, after you learn more about classes.


Lecture materials

Slides: individual slides for viewing , 6-up layout for printing

Examples: college_simple.py , college.py
To download the above .py files, right-click (Windows) or command-click (Macs) on the filename and then select "Save link as".
This way you choose where the files will be saved, instead of having your computer save to the default folder (from which you will later have to move your files for organization).

Link to Lecture Recording