How To Make Your Code Better with Refactor School, Free Episode 1

I’m going to publish another series of videos from Learn Code Live where I refactor one of the student’s code.  This code is exactly what he was expected to do when going through Learn Python The Hard Way, so really nothing wrong with it.  But, I took his code and pushed it as far as I could toward “professional”.  This series is 4 videos and you can watch the first one here:

Or, you can view it on tv.learncodethehardway.com directly.

What is Refactor School?

One aspect of learning to code for a beginner is learning how to “speak” like a “real programmer”.  Normally they pick this up by either working at a job where people correct their code, or hanging out with other programmers online.  Many students though don’t have the time or luxury of those situations, so I do that for them live.  I find a volunteer, and I improve their code over the course of 2-4 sessions taking it as far as I can go.  I treat the sessions in much the same way I would if I was teaching a junior developer on the job, so you can watch this and get some idea of what that might be like.

If you are a beginner and you have some code you’d like to volunteer to refactor school then message me on the forum and we’ll talk about it.  I’m looking for volunteers and I’ll give you the videos with my refactoring in exchange for you letting me review and repair your code.  The process is not abusive or overly critical, and it’ll help you see how I might write your code.

If you do have code, make sure that it at least works first, and that I can get it somehow.  It should also be relatively small, so maybe 3-10 files of Python code.  Other than that, let’s talk and work something out.

Free Short Course for Understanding Object Oriented Programming in Python

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:


machine.process(engine.operate().continue())

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:

  1. Variables
  2. Calling functions
  3. Modules
  4. If-statements
  5. Dicts

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:

  1. 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.
  2. 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.
  3. 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.

Seminar 1

Seminar 2

Seminar 3

Seminar 4

Seminar 5

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.