Object Oriented Programming (OOP) Flashcards
class
Tell Python to make a new kind of thing.
object
Two meanings: the most basic kind of thing, and any instance of some thing.
instance
What you get when you tell Python to create a class.
def
How you define a function inside a class.
self
Inside the functions in a class, self is a variable for the instance/object being accessed.
inheritance
The concept that one class can inherit traits from another class, much like you and your parents.
composition
The concept that a class can be composed of other classes as parts, much like how a car has wheels.
attribute
A property classes have that are from composition and are usually variables.
is-a
A phrase to say that something inherits from another, as in a “salmon” is-a “fish.”
has-a
A phrase to say that something is composed of other things or has a trait, as in “a salmon has-a mouth.”
class X(Y)
“Make a class named X that is-a Y.”
class X(object): def \_\_init\_\_(self, J)
“class X has-a __init__ that takes self and J parameters.”
class X(object): def M(self, J)
“class X has-a function named M that takes self and J parameters.”
foo = X()
“Set foo to an instance of class X.”
foo.M(J)
“From foo get the M function, and call it with parameters self, J.”
foo.K = Q
“From foo get the K attribute, and set it to Q.”
implicit inheritance
is-a
child classes inherit from parent class even when nothing is defined in child class.
class Parent(object): def implicit(self): print "PARENT implicit()"
class Child(Parent): pass
dad = Parent() son = Child()
dad. implicit()
son. implicit()
inheritance: override explicit
is-a
child class functions with same name override parent class inheritance
class Parent(object): def override(self): print "PARENT override()"
class Child(Parent): def override(self): print "CHILD override()"
dad = Parent() son = Child()
dad. override()
son. override()
displays- PARENT override() CHILD override()
inheritance: alter before or after
is-a
overriding where you want to alter the behaviour before or after the Parent class’s version runs. You first override the function, then you use a Python built-in function named ‘super’ to get the Parent version to call.
class Parent(object): def altered(self): print "PARENT altered()"
class Child(Parent): def altered(self): print "CHILD, BEFORE PARENT altered()" super(Child, self).altered() print "CHILD, AFTER PARENT altered()"
dad = Parent() son = Child()
dad. altered()
son. altered()
displays- PARENT altered() CHILD, BEFORE PARENT altered() PARENT altered() CHILD, AFTER PARENT altered()
super
super command enables program to find parent class functions from multiple parent classes and decide the correct order to run them using a special algorithm.
class SuperFun(Child, BadStuff): pass
using super with __init__ function
do some things in a child, then complete the initialization in the parent.
class Child(Parent): def \_\_init\_\_(self, stuff): self.stuff = stuff super(Child, self).\_\_init\_\_()
This is pretty much the same as the Child.altered
composition
has-a
Inheritance is useful, but another way to do the exact same thing is just to use other classes and modules, rather than rely on implicit inheritance. If you look at the three ways to exploit inheritance, two of the three involve writing new code to replace or alter functionality. This can easily be replicated by just calling functions in another class.
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): print "CHILD, BEFORE OTHER altered()" self.other.altered() print "CHILD, AFTER OTHER altered()"
son = Child()
son. implicit()
son. override()
son. altered()
In this code I’m not using the name Parent, since there is not a parent- child is- a relationship. This is a has- a relationship, where Child has- a Other that it uses to get its work done. When I run this, I get the following output:
OTHER implicit() CHILD override() CHILD, BEFORE OTHER altered()
When to Use Inheritance or Composition.
The question of inheritance versus composition comes down to an attempt to solve the problem of reusable code. You don’t want to have duplicated code all over your software, since that’s not clean and effi cient.
Inheritance solves this problem by creating a mechanism for you to have implied features in base classes.
Composition solves this by giving you modules and the ability to simply call functions in other classes. If both solutions solve the problem of reuse, then which one is appropriate in which situations?
guidelines for when to do which:
Avoid multiple inheritance at all costs, as it’s too complex to be useful reliably. If you’re stuck with it, then be prepared to know the class hierarchy and spend time fi nding where everything is coming from.
Use composition to package up code into modules that are used in many different unrelated places and situations.
Use inheritance only when there are clearly related reusable pieces of code that fi t under a single common concept or if you have to because of something you’re using.
do not be a slave to these rules.