LPTHW Exercises 40-44
(12/9/2013 -- 6:30PM PST)
Object Oriented Programming
Review
- Name all known Python data types
- What does mutability mean?
- Accessing items
- Built-in functions in advanced data types
Exercise 40: Introduction to classes
Complex data type (structure)
allows us to model the real world
How would we represent a basketball game in a program?
class BasketballGame(object):
def __init__(self, teams):
self.teams = teams
self.score = 0
def score_basket(self):
self.score += 2
def print_score(self):
print self.score
Exercise 40: Continued
Classes are similar in purpose and mechanics to modules
- Makes code reusable
- Preserves state (unlike functions)
- Attributes accessed through dot notation
Unlike modules, which are imported only once,
classes can be used to create a nearly infinite number of (largely) independent versions of themselves (objects)
Exercise 40: Continued (Objects)
A class is a blueprint which defines all attributes.
We use that blueprint to create objects, which are (largely) independent instances of the class
class Human(object):
def __init__(self, name):
self.name = name
def print_name(self):
print self.name
tom = Human("Tom")
loraine = Human("Loraine")
tom.age = 30
print tom.age, loraine.age
Extra Content: class properties
We said previously that objects are largely independent.
It is possible to share state within a Class.
class Human(object):
expected_lifetime = 80
def __init__(self, name):
self.name = name
tom = Human("Tom")
loraine = Human("Loraine")
print tom.expected_lifetime, loraine.expected_lifetime
extra content: objects everywhere
Everything in Python is an object
"strings are objects with properties and methods"
["lists are objects"]
{"as_are": "dictionaries"}
5 ### same with integers, floats, and Booleans -- they're more complicated
def even_a_func_is_an_object():
print True
even_a_func_is_an_object.really = True
exercise 41: Terminology
- class
- object (two meanings)
- instance
- def
- self
- inheritance
- composition
- attribute
- is-a
- has-a
Exercise 42: Intro to Inheritance
class Animal(object):
def __init__(self, name):
self.name = name
class Fish(Animal): def __init__(self, name, habitat): super(Fish, self).__init__(name) self.habitat = habitat
class Salmon(Fish): def swim():
print "upstream"
class Halibut(Fish): def communicate():
print "yarp" class GeneticHybridFish(Salmon, Halibut): pass
fish = GeneticHybridFish("Roy", "Brazil")
Exercise 43: OOP Game Design
Write down, run, and understand the game on your own
We will be creating a similar game for HW
Exercise 44: Inheritance & composition
Inheritance allows a subclass adopt, override, or alter
properties and methods from a parent class
class Parent(object): def implicit(self): print "PARENT implicit()" class Child(Parent): pass
class OverRideParent(object):
def override(self):
print "PARENT override()"
class OverRideChild(Parent):
def override(self):
print "CHILD override()"
Exercise Continued: SUPER
class Child(Parent): def __init__(self, stuff): self.stuff = stuff super(Child, self).__init__()
When you inherit from base or parent classes and implicitly call parent methods, MRO (method resolution order) is used to determine where method names are to be found.
super allows you to explicitly call methods on parent classes, using the context of the child subclass instance
Exercise Continued: Composition
Composition is a way of combining objects to form larger, more complex data structures. Isolate logic into self-contained structs
class Other(object): def override(self): print "OTHER override()" def implicit(self): print "OTHER implicit()" def altered(self): print "OTHER altered()" class Child(object): def __init__(self): self.other = Other() def implicit(self): self.other.implicit() def override(self): print "CHILD override()" def altered(self): self.other.altered() son = Child() son.implicit() son.override() son.altered()
Exercise Continued: When to use
When to use inheritance vs. composition is a classic debate
LPTHW recommends that you
"Use inheritance only when there are clearly related reusable pieces of code that fit under a single common concept or if you have to because of something you're using"
It is also recommended that you avoid multiple inheritance whenever possible
LPTHW Exercises 40-44 (12/9/2013 -- 6:30PM PST)
By benjaminplesser
LPTHW Exercises 40-44 (12/9/2013 -- 6:30PM PST)
- 1,353