Feel Free to skip the whole intro by scrolling down to the picture and start reading below if you are in a rush :)
Backstory
Yesterday I had an argument with a friend I grew up with. He learned all the basics of python (Loops, Branching, Functions, etc). He also knew the Class
Keyword.
I tried to convince him, that he knew enough to write object oriented code in python while he adamantly insisted he did not know enough. It got intense and in the end he left to walk it off, while I sat down to put into writing what he needed to see how little he really was missing and that, knowing how to write a function and define a class, he was already able to use the most basic version of OOP for his code.
The Problem
I do not think it was his fault for thinking that there was some arcane knowledge he had to first unlock in order to be able to say he knows OOP.
In my opinion the fault lies with the majority of "educators" in the space who overcomplicate principles or try to cram every nuance into something meant to teach just the fundamental principle to a bloody beginner.
The Solution?
I translated what I wrote for him. If the heartfelt thank you I received today was any indicator i succeeded in communicating how simple this "advanced" principle actually is. If you know Python basics and maybe programmed a few small things like a calculator or something useful, then you should be able to understand this.
In Software engineering, everything is a copy
In the end there is nothing really new in programming from start to finish. Everything is just an abstraction or a bigger/more powerful version of a thing that already exists.
- Variables are containers for a value
- datastructures like a list or a dictionary are containers for many values
- functions are containers for logic
- classes are just the next bigger container for all the other containers
- a python file is just another container for any code or commentary.
Enough yapping - here you go :)
Vocabulary
- Class = Blueprint
- Object = Thing built from a blueprint
- Method = Function that was put into a class, but we don't want to call it a function...
- Initialize = Prepare
Why
Classes are used to package together functions and values that belong together so that they can be easily copied, extended and reused.
How
Here's a class that David could write and use with his current knowledge:
class Car:
def drive():
print('hello world')
Here's the code you need to use the class:
myCar = Car() # create an object from the class
myCar.drive() # prints hello world
BUT THERE'S MORE
Yes:
- Inheritance (Class Mother passes on her skills and properties to Class Son)
- Polymorphism (Son interprets something Mother taught him differently and does it his way from now on)
- Initialization (Son learns Python to later know the minimum he will use as a programmer)
Inheritance:
If I write the name of another class in brackets after class Car
, my new class can use all the functions of the other one.
class Vehicle:
def drive():
print('hello world')
class Car(Vehicle):
def honk():
print('honk')
myCar = Car() # create an object from the class
myCar.drive() # prints hello world
myCar.honk() # prints honk
Polymorphism:
A complicated word for "I can overwrite things from the other class in my new class
class Vehicle:
def drive():
print('hello world')
class Car(Vehicle):
def drive():
print('vroom')
If I now call Car.drive(), I get vroom
instead of hello world
. The inherited function drive from Vehicle was overwritten by the inheriting class Car with its own version of it.
And to make everyone think that OOP is super complex, we don't say overwrite function
, we say polymorphism
.
Initialization:
Sometimes you want certain things to be done or values to be set before the user of the class gets the chance to use the new object.
For this, there is __init__
. Init takes its own class as an argument as "itself" (self) so it can access everything in this class.
Self is a bit like doing myCar = Car() in order to be able to use myCar.drive() later.
Init is just a function with a special name. Python knows that when you create a new object and the class has an init function, it should first perform everything in init even if no one writes myCar.init().
class Car:
def __init__(self):
self.tank_is_full = True
self.has_wheels = True
self.number_of_wheels = 4
def drive():
if self.tank_is_full: # self is a variable for your class, you can only use it if you have given it to the function
print('hello world')
```python
def race(self):
if self.tank_is_full:
print('vroom')
myCar.drive() will fail with an error because it doesn't know the variable self in the function.
myCar.race() will print vroom
, because with self you can use everything from the class anywhere.
What would it look like without OOP?
tank_is_full = True
def drive():
if tank_is_full:
print('hello world')
def race(tank_is_full):
if tank_is_full:
print('vroom')
drive() will throw an error because you are using a variable you did not define in your function.
race(tank_is_full) will print vroom
because you threw the variable into the function when you called it.
But what magic function does OOP fulfill then?
There's no magic. A class is literally just another type of variable into which you can store anything you want for later use.
It's a container, of which you can make copies, of which you can overwrite parts, or which you can build upon by using it as a starting point.
That's all it is.
Aren't there other things one can learn about OOP?
About OOP itself? No. Concepts and coding styles around OOP? Yes - but then you are a Senior Developer (S.O.L.I.D. Principles, Object-Calisthenics, Code Smells, etc)
Post Scriptum
See - that wasn't so hard was it? All those fancy pants slinging their latin or greek derived words to sound smarter no longer sound that much smarter than you do they? Great! Language is really a tool, unfortunately instead of a tool for propagation of knowledge it has devolved into a tool for stroking your own ego for some professors and professionals. Yes it sounds smart if you use them, but if you can put it into words so simple a beginner can understand the core, then you are actually smart and have proven you understood them.
Now, in case I hurt one of those egos I just made fun of - don't get angry please. This is supposed to be light-hearted education, although i admit to my frustration over people who deliberately overcomplicate their vocabulary to sound smarter when they really aren't - i blame university :D
use language as a tool - not for ego
Of course I know that certain words were invented for precision and nuance or just to be able to replace a long description with a single word. I also know that historically certain language was used to deliberately prevent "outsiders" from getting the knowledge of a small "elite". If you teach - simplify, if you learn - find new problems.
Emergence of Nuance: Learning is a process that unfolds gradually, and nuances or advanced concepts should be introduced when learners have a solid grasp of the basics and can see the need for these finer details.