Session 18 Preparation — Videos, Reading and Quizzes
Do this Quiz on
Session 18
as you read the textbook and the web page on formatting.
Print it and complete it using pencil or pen, or fill it in electronically — your choice.
- Here is the quiz in Microsoft Word in case you want to fill it in electronically in that format.
- Defining Classes and Objects
- Textbook reading: Sections
9.1-9.6 — Objects and Classes (pages 444-460).
You saw many of these ideas "by example" in your Session 16
class; now it is time to look at the details.
- As you read, focus your attention on:
- Overview and terminology (Section 9.1). After reading this, you should be able to give a basic
definition, in your own words, of the terms class,
public interface, and encapsulation.
These terms will be elaborated in subsequent sessions of this chapter.
- Instance variables and self (pages 447-449). Instance variable is
another name for what we have called a field. An object is an instance of its class, and so a
variable that belongs to a particular object (something that each object form the class needs to remember about itself) is an
instance variable.
Also, an object needs a way of referring to itself.
The special variable name self is used for this purpose.
Section 9.2 has lots of details about both instance variables and self.
- Public interfaces, mutators and accessors (section
9.3).
- The public and private parts of a class (page 454).
Python does not enforce this (other languages, such as
Java, C++, and C#, do), but it is still a good idea.
- What is the purpose of a class's constructor?
(section 9.5) Why should yo never call __init__
directly? How are default and named arguments
useful when writing constructors (and functions in
general)?
- Implementing Methods (Section 9.6) WHy do we need to
use self so much in the body of a method?
How to call a method within a method (for the same
object, for a different object).
- Only define a class's instance variables in the
constructor (page 460)
- Class variable (a variable that belongs to the
class, rather than having one variable for each object
of that class) (page 460).