Why do I care about
classes?
• Code organization
• Readability
• Making large-scale changes easier
• What everybody does these days
• Encapsulation (SAT word!)
Tuesday, April 24, 12
What is a class?
Factory for creating nouns
based on your description
Tuesday, April 24, 12
What is a class?
Factory for creating nouns
based on your description
Tuesday, April 24, 12
What does an object
contain?
Local Data
Tuesday, April 24, 12
What does an object
contain?
Local Data
Name: David
Birthday: Dec 18
Job: Software Engineer
Tuesday, April 24, 12
What does an object
contain?
Local Data
Name: David
Birthday: Dec 18
Job: Software Engineer
Actions
say_hello()
eat_spaghetti()
teach_a_class()
Tuesday, April 24, 12
What does an object
contain?
Local Data Local Data
Name: David Name: Anna
Birthday: Dec 18 Birthday: Sep 6
Job: Software Engineer Job: Entrepreneur
Actions
say_hello()
eat_spaghetti()
teach_a_class()
Tuesday, April 24, 12
What does an object
contain?
Local Data Local Data
Name: David Name: Anna
Birthday: Dec 18 Birthday: Sep 6
Job: Software Engineer Job: Entrepreneur
Actions
say_hello()
eat_spaghetti()
teach_a_class()
Tuesday, April 24, 12
Syntax
Definition
class User(object):
name = "Jack"
birthday = "Jan 2"
job = "student"
def say_hello(self):
print "Hello, I'm " + self.name
def eat_spaghetti(self):
print "Yum!"
Tuesday, April 24, 12
Banking Program
Objects we’ll need:
• Banks
• Users
• Accounts
Tuesday, April 24, 12
Main Program
• program:
•bank classbank object.method.
create
• create create user object.
with log in
•user class with name & password
• create log in.
•account class with amount
• create create account object.
•
• add money.
Tuesday, April 24, 12
Main Program
• program:
•bank classbank object.method.
create
• create create user object.
with log in
•user class with name & password
• create log in.
•account class with amount
• create create account object.
•
• add money.
Tuesday, April 24, 12
Main Program
• program:
•bank classbank object.method.
create
• create create user object.
with log in
•user class with name & password
• create log in.
•account class with amount
• create create account object.
•
• add money.
Tuesday, April 24, 12
Main Program
• program:
•bank classbank object.method.
create
• create create user object.
with log in
•user class with name & password
• create log in.
•account class with amount
• create create account object.
•
• add money.
Tuesday, April 24, 12
Changes!
The government has just declared that
all bank deposits will be taxed %5.
Tuesday, April 24, 12
Changes!
The government has just declared that
all bank deposits will be taxed %5.
Oh no!
Tuesday, April 24, 12
Changes!
The government has just declared that
all bank deposits will be taxed %5.
Oh no!
Do we have to rewrite our entire program?
Tuesday, April 24, 12
Changes!
The government has just declared that
all bank deposits will be taxed %5.
Oh no!
Do we have to rewrite our entire program?
Nope!
Tuesday, April 24, 12
Original Version
class Account(object):
balance = 0
def deposit(self, amount):
self.balance = self.balance + amount
def withdraw(self, amount):
self.balance = self.balance - amount
Tuesday, April 24, 12
New Version
class Account(object):
balance = 0
def deposit(self, amount):
tax = amount * 5/100
remaining = amount - tax
self.balance = self.balance + remaining
def withdraw(self, amount):
self.balance = self.balance - amount
Tuesday, April 24, 12