Object Oriented Programming
Preview for Python 101
(12/9/2013 -- 6:30PM PST)
Exercise 40: Introduction to classes
Data structure which 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 and behavior.
For example, we could describe chairs through classes
4 legs, 1 back, intended use is for sitting, etc...
We use that blueprint to create objects, which are (largely) independent instances of the class
My particular IKEA chair is an object of the chair Class.
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 (even classes)
"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")
Inheritance
Inheritance allows a subclass adopt, override, or alter
properties and methods from a parent class
Inheritance Continued: SUPEr
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
Composition
Composition is a way of combining objects to form larger, more complex data structures.
Isolate logic into self-contained structs
Inheritance vs. Composition
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
Intro to Object Oriented Programming in Python
By benjaminplesser
Intro to Object Oriented Programming in Python
- 3,555