Lecture notes for the Introduction to Computer Science I by James Tam Return to the course web page

Example Programs: Classes And Objects

Description

Program name

List of list: each list element consists of a list.

List_of_lists.py

A simple class definition for a 'client' and the creation (instantiation) of an object of that class.

Client.py

A class definition that includes method definitions for the class.

Person.py

An example illustrating the use of the parameter that is passed into all methods 'self'

Person2.py

An example illustrating the use of the initialization method '__init__'

Init_method.py

The initialization method with default parameters.

Init_method2.py

Module example: functions are divided among two modules modules.zip
Module example: each class definition resides in it's own module modules2.zip

(Extra example: not in the notes) how to handle the case where a method of a class takes as a parameter an object of that same class.

class_parameters.py

(Extra example: not in the notes) what is the purpose of the __str__ method. str.py
Smorgasbord example: another one to illustrate: exception handling (this time where the instruction that can cause the exception to be raised is not a function call), a contrast of a function vs. method call. smorgasborg.py