Class Notes (1,100,000)
CA (620,000)
UTSC (30,000)
All (10)
Lecture 11

CSCA08 Lecture 11: Week 11 Notes

Computer Science
Course Code

This preview shows pages 1-2. to view the full 6 pages of the document.
Alexander Magony
CSC108H1F 2014
Week 11
Creating a New Type
-We’re going to create a program that emulates a cash register. We’re going to start off with 5 loonies,
toonies, fives, tens, and twenties, print the total value ($190), and then add 3 toonies and removes 2
First, we need to
define a custom
-Defining the type CashRegister:
The __init__ method is called to initialize a new CashRegister object. Since it’s a method (I guess
since it is a function that is tied to this type), the first parameter is self (the object being initialized, in
the examples: register). We are also including every cash variety as a parameter (loonies, toonies,
fives, tens, twenties).
If we want to create variables inside the object, we include the code self.loonies = loonies, for
example, so that the number of loonies we pass as an argument is stored in self.loonies. These
variables inside objects are called instance variables since they only belong in an object/instance.
Each object is an instance of its
This is a bit confusing so I’m going
to try and explain it how I understand
-We want to create type/class
CashRegister. When we make an
object of type CashRegister (eg.
register = CashRegister(5, 5, 5, 5,
5), we want to also create
instance variables for it.
-A method is a function that
belongs inside a class/type.
Example, append only worked
when used with type list.
-We want to automatically run a
method when we create an object
of type CashRegister that creates the instance variables: self.loonies, self.toonies, self.fives,
self.tens, self.twenties. If our object is register, self refers to register (eg. register.loonies). To
automatically run this method, we define it as __init__ (meaning it automatically initializes as
opposed to having to be called like list’s append).
-Objects are instances of its class. I guess that’s why the variables that belong to these objects/
instances are called instance variables.
-If we run the code at the top of the page, we get an AttributeError for get_total since we have not
created that attribute for the object register yet.

Only pages 1-2 are available for preview. Some parts have been intentionally blurred.

Alexander Magony
CSC108H1F 2014
- The code to the right shows how instance variables can be
called for an object after it is defined. Creating another object
will store it at a different memory address so any changes to
the instance variables will only occur to the object that the
change is applied to (eg. making register2.twenties refer to 3).
-Now let’s define the method get_total to remove the
error. Again, the parameter is self, since we are
calculating the total of the object of type
We simply return the sum of all of the instance
variables multiplies by their value
-If we run again, we get another AttributeError for not
having an add method, which adds some amount of a certain
coin/bill. So let’s write that.
add has three parameters, self, count, and denomination.
This method doesn’t refer to anything, it just mutates the
existing object by adding the count amount of the specific
We simply write an if/elif statement checking to see what
denomination is indicated so that the count can be added
to the existing amount of that denomination.
-For method subtract, it’s the exact same code except
subtracting the count rather than adding it.
-Now that we’ve written all four methods (__init__, get_total, add, remove) of the class CashRegister,
we get the appropriate printed
values without error when
rerunning the program.
Plugging into Python Syntax
-We’ve added numbers or strings together using +, multiplied ints or ints with strings, etc. We’re going to
explore how we can plug these operators into our custom classes.
-Recall WordplayStr, a class based on class string with one method (same_start_and_end which
reports if the first and last character is the same).
-It turns out all classes are based on a class object which is the
most base type (every other class automatically derives from this
class). Contains many special methods (indicated by beginning
and ending with two underscores, like __init__).
You're Reading a Preview

Unlock to view full version