Reading assignment

Readings in the "Think Python 2e" book for CIS 40

Unit 10 Object Oriented Programming

It is best if you read the Introduction to methods and example section at the bottom of this page, before reading the material from the book and slides.

Chapter 15 Classes and objects
15.1 Programmer-defined types
15.2 Attributes
15.5 Objects are mutable

Chapter 17 Classes and methods
17.5 The __init__ method
17.6 The __str__ method

Introduction to methods and example

We used several methods in Unit 5 Graphics. Member functions are used in Unit 5. Member function is an alternate name for a method; in Python and many programming languages they are two names for the same thing. Review Unit 5 to remember how to use methods, if you need to.

The slides and the book talk about putting a class in a file. We will write the class in the same file with the rest of your code; this is simpler. An example showing this approach can be downloaded from Canvas. The file name in Canvas is CIS40_Module_10_Example.py

To copy this file from Canvas to your local working directory:
Log in to Catalyst.
Select CIS 40.
Select files.
Select CIS40_Module_10_Example.py
Move your cursor to the right of the green checkmark icon.
Click the down arrow next to the gear.
Click Download
Click Save File and OK
The file should be on your desktop or download directory

Alternately, you may copy and paste the code, which follows.

#Module 10 example
#This is an example that clarifies certain points that
#may not be clear from the slides and the book.
#It is a complete example of both Class code as well as code using the Class.

class BankAcct:

    #The __init__ method doesn't create an object.
    #It is simply an optional (but useful!)
    #way to initialize selected values specific to
    #the object that is in the process of being created.
    def __init__(self, num, bal):
        self.num = num
        self.bal = bal

    #The __str__ method is an optional way to print
    #selected information about the object.
    #If the __str__ method were omitted and you were to print
    #the object, you would get something like this:
    #<__main__.BankAcct object at 0x01A4BB10>
    #The primary purpose of the __str__ method is for debugging,
    #not for general use within the program.
    def  __str__(self):
        return "Account: " + str(self.num) + "\nBalance: " + str(self.bal)

    def getNumber(self):
        return self.num

    def getBalance(self):
        return self.bal

    def deposit(self, amt):
        self.bal = self.bal + amt
    

#A single script file can contain both a class definition as well
#as code that uses that class - no import is required in this case.

#instantiation of myAcct (this uses the __init__ method)
#with acct num 123 and initial balance of $50
myAcct =  BankAcct(123, 50)

#print myAcct (this uses the __str__ method)
print(myAcct)

#deposit $100
myAcct.deposit(100)

#print acct balance
print("New balance is:", myAcct.getBalance())

#now we're going to work with a second account
secondAcct = BankAcct(456,1000)

#We can print the two accounts to see that they are
#each independant of each other
print(myAcct)
print(secondAcct)

# end of example