LPTHW Review
Object Oriented Programming Review
Terminology Review
class
object
instance
def
self
inheritance
composition
attribute
is-a
has-a
class vs. Object (instance)
How do classes and objects relate to each other?
What data does a class hold vs. an object?
How do we get an object's data (properties and methods)?
What if we want to share state among all instances of a particular class?
Inheritance
What is inheritance (see IDE)?
Why do we use inheritance?
How do we inherit from a base (parent) class?
How do you inherit from two base classes?
What is super?
Inheritance Continued: SUPER
class Child(Parent):
def __init__(self, stuff):
self.stuff = stuff
super(Child, self).__init__()
When you inherit from a parent class and implicitly call parent methods, MRO (method resolution order) is used to determine where method names are found.
super allows you to explicitly call methods on parent classes, using the context of the child subclass instance
Composition
What is composition (see IDE)?
Why do we use composition?
How would we use composition to represent a test score on a term paper and a student?
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 concept or if you have to because of something you're using"
It is also recommended that you avoid multiple inheritance whenever possible
Modules
Python files loaded into a program
and converted into objects
We access variables and functions from a
module as we would an attribute on an object (dot notation)
Why do we use modules?
As external and internal libraries