Janet Riley @janet_riley_net
These slides are from a lightning talk I gave to Boston PyLadies. The examples are geared to Python, but the content applies to languages that have classes and modules.
Press the space bar to advance sequentially.
Use the blue arrows in the corner to dive into a section or skip around.
Speaker notes are overlaid in grey.
the eternal
dream
class Duck:
def looks_like(self):
print("You see here a duck.")
def swim(self):
print("Paddle, paddle, paddle")
def quack(self):
print("Quack!")
Just like the parent class, except a little different
specialization
add * extend * override
>>> plain_dict = dict()
>>> print( plain_dict['no_such_key']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'no_such_key'
KeyError: 'no_such_key'
defaultdict inherits from dict.
It's just like its parent, with one difference:
>>> import collections
>>> my_default_dict = collections.defaultdict(int)
>>> print( my_default_dict['no_such_key'])
0
Python dict() is a key-value map.
It raises an error if we access a key that doesn't exist.
Substitution
Dr. Liskov
look like a duck
swim like a duck
quack like a duck
✓
A substitute duck must:
✓
✓
The 'L' in the SOLID principle
have duck DNA
✓
And in some languages,
import RubberDucky
class Ernie:
def __init__(self):
self.ducky = RubberDucky()
def sing(self):
print("Rubber ducky, you're the one")
print( self.ducky.quack())
print( self.ducky.quack())
print("You make bath time lots of fun")
Ernie is not a duck.
group related code into namespaces
which keeps them tidy
import json
duck = json.loads('{"name":"Daffy Duck", \
"species":"Loony Tune"}')
class Trogdor(Dragon, Man):
def burninate(self):
pass
def shake_hands(self):
pass
"Trogdor was a Man!
He was a Dragon Man!
Maybe he was just a dragon..."
Platypus
inherits bill, eggs from Duck
inherits fur, tail from Beaver
inherits poison spurs from Ninja
The Art of Subclassing - Raymond Hettinger
Stop Writing Classes - Jack Diederich
Practical Object-Oriented Design in Ruby - Sandi Metz
An articulate and approachable guide to object design. If you know Python, you'll understand the Ruby well enough.
Trogdor the Burninator - HomeStarRunner.com
See the social and domain forces that lead to strange inheritance trees.