1 15-ClassesInheritance


Previous: 14-Recursion.html

**"The great thing about Object Oriented code,**
 **is that it can make small, simple problems **
 **look like large, complex ones..."**

What to do with that sarcasm??
* We’ll over-use classes while learning them, but after we complete this section and have have learned classes, like with functions, only use classes when you actually need them!
* OOP is a massively foundational component of modern programming, and you absolutely need to know it well to get by as a programmer.
* It is also ideal and efficient for some larger projects, take for example neural network simulators I’ve programmed (with extensive use of OOP).

1.1 Screencasts

Lecture start/end points are noted below in the notes and code.
* Lecture 1 (Classes, intro to inheritance): https://vimeo.com/523747025
* Lecture 2 (More inheritance): https://vimeo.com/524989991
* [ ] Lecture 3 (realistic example: TODO
* Tip: If anyone want to speed up the lecture videos a little, inspect the page, go to the browser console, and paste this in:
document.querySelector('video').playbackRate = 1.2

1.2 Introduction

https://en.wikipedia.org/wiki/Programming_paradigm
Two types of imperative paradigms include:

1.2.1 1. Procedural programming

https://en.wikipedia.org/wiki/Procedural_programming
* A common programming paradigm is procedural programming which structures a program like a recipe in that it provides a set of steps, in the form of functions and code blocks, which flow sequentially in order to complete a task.

1.2.2 2. Object oriented programming (OOP)

https://en.wikipedia.org/wiki/Object-oriented_programming
* Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which can contain:
* data, in the form of fields (often known as member attributes, data members, or member properties), and
* code, in the form of procedures (often known as member methods or member functions).
* The object’s procedures (member functions) can access and potentially modify the data fields of the object with which they are associated.
* Objects often have a notion of “this” or “self”.

1.2.3 Python’s imperative paradigms?

1.3 1. Procedural python

You’ve mastered this with basic statements and functions already!

1.4 2a. Python OOP: basics

Python Object-Oriented Programming, and the acronym is…

1.4.1 Extra reading

Note: you can dive particularly deep on this topic, so don’t worry if some of the more advanced links seem a bit too advanced.
* http://scipy-lectures.org/intro/language/oop.html
* https://books.trinket.io/pfe/14-objects.html
* https://docs.python.org/3/tutorial/classes.html
* https://python.swaroopch.com/oop.html
* https://realpython.com/python3-object-oriented-programming/
* https://www.learnpython.org/en/Classes_and_Objects
* https://www.learnpython.org/en/Code_Introspection
* https://www.listendata.com/2019/08/python-object-oriented-programming.html
* https://www.python-course.eu/python3_object_oriented_programming.php
* https://www.python-course.eu/python3_classes_and_type.php
* https://www.python-course.eu/python3_class_and_instance_attributes.php
* https://www.python-course.eu/python3_inheritance.php
* https://www.python-course.eu/python3_inheritance_example.php
* https://www.python-course.eu/python3_multiple_inheritance.php
* https://www.python-course.eu/python3_multiple_inheritance_example.php
* https://www.tutorialspoint.com/python3/python_classes_objects.htm

1.4.2 What are objects?

1.4.3 Advantages of OOP

1.4.4 What are classes

1.4.5 In python, everything is a class!

**"One of my goals for Python was to make it so that all objects were first class. By this, I meant that I wanted all objects that could be named in the language (e.g., integers, strings, functions, classes, modules, methods, and so on) to have equal status. That is, they can be assigned to variables, placed in lists, stored in dictionaries, passed as arguments, and so forth." **
~** Guido van Rossum: Blog, The History of Python, February 27, 2009**

Check out the attached code for the day, and readings.

Basic python already has been using clasess:
* 15-ClassesInheritance/classes_00.py

The image below is a class diagram:
* The top half shows data members.
* The bottom half shows function members (none on this one).
15-ClassesInheritance/classes_01.py_pyreverse.svg
* 15-ClassesInheritance/classes_01.py

This class actually has a member function too:
15-ClassesInheritance/classes_02.py_pyreverse.svg
* 15-ClassesInheritance/classes_02.py

This one is a bit more realistic:
* What about the special underscore functions?
15-ClassesInheritance/classes_03.py_pyreverse.svg
* 15-ClassesInheritance/classes_03.py

Note: You can generate the class diagrams above, or from any code like this:
$ pyreverse --filter-mode=ALL -o svg <yourfile.py>

++++++++++++++++++
Cahoot-15.1
https://mst.instructure.com/courses/58101/quizzes/56485

++++++++++++++++++
Cahoot-15.2
https://mst.instructure.com/courses/58101/quizzes/56486

++++++++++++++++++
Cahoot-15.3
https://mst.instructure.com/courses/58101/quizzes/56487

1.5 2b. Python OOP: Inheritance

15-ClassesInheritance/inherit.jpg

1.5.1 What

1.5.2 Why

  1. Code re-usability: When a new class inherits an existing class, all its methods and fields become available in the new class.
  2. Makes it easier to create and maintain large applications.
  3. Allows abstract classes as interfaces (more coming up soon).

1.5.3 Basic terminology

Parent/super/base-class
15-ClassesInheritance/inheritance-example.jpg
Child/sub/derived-class

The convention is to have the arrow pointing toward the parent class, though that may be counter-intuitive, and different authors switch it (some do below).

Check out more code:
15-ClassesInheritance/classes_04.py_pyreverse.svg
* 15-ClassesInheritance/classes_04.py

1.6 +++++++ Lecture 2 starts here

(note also in code about end-point).

1.6.1 Types of inheritance

1.6.1.1 Simple / single

One derived class inherits from only one base class.
It is the simplest form of Inheritance.
15-ClassesInheritance/single-inheritance.jpg

++++++++++++++++++
Cahoot-15.4
https://mst.instructure.com/courses/58101/quizzes/56488

1.6.1.2 Hierarchical

Multiple derived classes inherits from a single base class.
15-ClassesInheritance/hierarchical-inheritance.jpg
For example:
15-ClassesInheritance/inheritance.png

1.6.1.3 Multiple

A single derived class may inherit from two or more than two base classes.
15-ClassesInheritance/multiple-inheritance.jpg

1.6.1.4 Multi-level

Derived class inherits from a class, which in turn inherits from some other class.
The Super class for one, is sub class for the other.
15-ClassesInheritance/multilevel-inheritance.jpg

1.6.1.5 Hybrid

Hybrid Inheritance is combination of Hierarchical and Muti-level inheritance.
15-ClassesInheritance/hybrid-inheritance.jpg

1.6.2 Name resolution

Recall, names are resolved through consistent procedures.

First, L.E.G.B. within functions:
15-ClassesInheritance/legb.png
Next, functions in classes have their local scope searched first, like above, and instance members within classes are searched before class members, before global, before built-in:
15-ClassesInheritance/class.png
Further, when using . notation from an instance object, the object itself is searched first, then derived classes are searched before base classes. Method Resolution Order (MRO) specifies order of resolution within inheritance. When we inherit from multiple classes, they are searched child-first, left to right, as you specified them in the class definition.
* http://www.srikanthtechnologies.com/blog/python/mro.aspx
* https://www.geeksforgeeks.org/method-resolution-order-in-python-inheritance/
* https://data-flair.training/blogs/python-multiple-inheritance/
* https://www.python.org/download/releases/2.3/mro/
* https://medium.com/@__hungrywolf/mro-in-python-3-e2bcd2bd6851
If you ever forget this, you can just check the MRO:
print(YourClassName._mro_)

1.6.3 How

Check out the code above, and readings.
15-ClassesInheritance/classes_05.py_pyreverse.svg
* 15-ClassesInheritance/classes_05.py

15-ClassesInheritance/classes_06.py_pyreverse.svg
* 15-ClassesInheritance/classes_06.py

15-ClassesInheritance/classes_07.py_pyreverse.svg
* 15-ClassesInheritance/classes_07.py

++++++++++++++++++
Cahoot-15.5
https://mst.instructure.com/courses/58101/quizzes/56489

15-ClassesInheritance/hh1.jpg
15-ClassesInheritance/hierarchical-inheritance-example.jpg

1.7 Review of terms

1.8 Review of concepts

**"The problem with object-oriented languages is they've got all this implicit environment that they carry around with them. You wanted a banana, but what you got was a gorilla holding the banana, and the entire jungle."**
        - Joe Armstrong

In practice, this is very true…

Next: 16-TypeFormatDebug.html