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).
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
https://en.wikipedia.org/wiki/Programming_paradigm
Two types of imperative paradigms include:
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.
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”.
You’ve mastered this with basic statements and functions already!
Python Object-Oriented Programming, and the acronym is…
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
**"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
This class actually has a member function too:
* 15-ClassesInheritance/classes_02.py
This one is a bit more realistic:
* What about the special underscore functions?
* 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
Parent/super/base-class
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
(note also in code about end-point).
One derived class inherits from only one base class.
It is the simplest form of Inheritance.
++++++++++++++++++
Cahoot-15.4
https://mst.instructure.com/courses/58101/quizzes/56488
Multiple derived classes inherits from a single base class.
For example:
A single derived class may inherit from two or more than two base
classes.
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.
Hybrid Inheritance is combination of Hierarchical and Muti-level
inheritance.
Recall, names are resolved through consistent procedures.
First, L.E.G.B. within functions:
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:
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_)
Check out the code above, and readings.
* 15-ClassesInheritance/classes_05.py
* 15-ClassesInheritance/classes_06.py
* 15-ClassesInheritance/classes_07.py
++++++++++++++++++
Cahoot-15.5
https://mst.instructure.com/courses/58101/quizzes/56489
**"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