I’m going to start publishing free content for people to consume in areas that I find help people in general but don’t really fit into any of my own products. In this post I’m going to talk about OOP School, which was a 5 part series featured in LCTHW:Live. In this series of lessons I actually build a basic working OOP using only dicts and modules in 64 lines of Python code.
Probably the best video to watch and understand what is done in the 5 video series is Seminar #5, where I go back through the code and explain what I did:
The purpose of these lessons is to remove the magic behind Object Oriented Programming. Too often beginners see code like this:
and then shut down because they can’t follow what is going on. To me I know what the . (dot) does in every language I use, but that’s only because I grew up actually implementing the dot in a few languages that didn’t have OOP. I did this in C, Pascal, and I think I tried in BASIC but gave up. I forget, it was a long time ago because I’m old now.
This Learn Code Live seminar series starts with a person who knows the following:
- Calling functions
And using just that (plus some small jumps into other corners of Python), builds an OOP system that gives you the basics of Python’s OOP in 5 hours of work and 64 lines of code.
Objects Are Not Magic
I believe that programming languages like Python have a vested interest in making parts of their programming language mysterious and magic. What languages like Python do is hide mountains of operational code behind a single character: the dot. This seemingly tiny character unleashes potentially hundreds of thousands of lines of code inside Python without you being able to understand it.
In fact, the early creators of Python didn’t even understand it and had to rewrite the Object system at least 3 times. Many Python practitioners today think that there’s a huge difference between objects, classes, dicts, and modules. The only real difference between them is one of syntax and first class language support. You can completely implement objects and classes using just dicts and modules, and even inside every object and class hides a __dict__ which means they really are just dicts.
Object Oriented Programming is not magic. It’s not some truth of the universe or mystical thing only ordained Real Programmers understand. OOP is code that someone wrote which gives you three things:
- Syntax – You don’t have to type out a function name like get(), new(), set(), inherits() to work with objects and classes. You just use . (dot), parenthesis, equals, and other symbols and Python does the rest for you.
- Conventions – You don’t have to remember to call certain functions in the right order to talk to another person’s code. Because Python gives you syntax it forces everyone to follow the same conventions so their objects work together.
- Parsimony — You don’t have to manually write out which classes inherit from other classes or explicitly name every method that has to change in a subclass. Python saves you from having to repeatedly type out boilerplate because it has syntax and conventions to do all the for you, and to make everyone else do it too.
All of these things are just social constructs, they are not core truths of a computer. They are not so huge an advantage that you can’t sit down and build your own replica of these concepts and come up with your own OOP system. As long as you have some kind of function and some kind of dict you can make an object system. I mean, how do you think Python has an OOP system? Magic elves built it?
When it comes down to it, an Object Oriented Programming system just an application of data and functions to enforce a convention through syntax that allows multiple humans to share code in a parsimonious way. You can build your own and any language or programmer who tries to tell you that you can’t build an OOP system from first principles that replicates the one they use probably has no idea how theirs works.
You know, like the people who invented Python.
The Full Series
By watching this series of videos you can get at least a tiny idea of what is going on behind the dot and understand the basic concepts of OOP before trying to actually learn it. It’s only 64 lines of code (available here) that is simple enough for a beginner to understand, and not full of extraneous features not many people use like multiple inheritance.
You can watch the videos below, or go to tv.learncodethehardway.com to view them.
Joining Learn Code Live
This is an example of the kind of hacking and teaching I do on Learn Code Live. If you’d like to join my new Learn Code Live and get in on the Python Basics Workshop I’m going to run then you can Signup Here and you can see a searchable list of everything already recorded that you get after you signup. The next class is going to be this Saturday at 12pm EST. Hope to see you there.