Object Oriented Programming (OOP) Flashcards

1
Q

class

A

Tell Python to make a new kind of thing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

object

A

Two meanings: the most basic kind of thing, and any instance of some thing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

instance

A

What you get when you tell Python to create a class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

def

A

How you define a function inside a class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

self

A

Inside the functions in a class, self is a variable for the instance/object being accessed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

inheritance

A

The concept that one class can inherit traits from another class, much like you and your parents.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

composition

A

The concept that a class can be composed of other classes as parts, much like how a car has wheels.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

attribute

A

A property classes have that are from composition and are usually variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

is-a

A

A phrase to say that something inherits from another, as in a “salmon” is-a “fish.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

has-a

A

A phrase to say that something is composed of other things or has a trait, as in “a salmon has-a mouth.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

class X(Y)

A

“Make a class named X that is-a Y.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
class X(object): 
    def \_\_init\_\_(self, J)
A

“class X has-a __init__ that takes self and J parameters.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
class X(object): 
    def M(self, J)
A

“class X has-a function named M that takes self and J parameters.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

foo = X()

A

“Set foo to an instance of class X.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

foo.M(J)

A

“From foo get the M function, and call it with parameters self, J.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

foo.K = Q

A

“From foo get the K attribute, and set it to Q.”

17
Q

implicit inheritance

is-a

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()

18
Q

inheritance: override explicit

is-a

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()
19
Q

inheritance: alter before or after

is-a

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()
20
Q

super

A

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
21
Q

using super with __init__ function

A

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

22
Q

composition

has-a

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()
23
Q

When to Use Inheritance or Composition.

A

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.