Tuesday 19 March 2019

Love letter to Python Classes

So, after about 5 years into python I started to understand and use classes a lot at DNEG. There were a few things that I never understood about classes,and subsequentially went through the 9 stages of coding class-denial:
  • Shock: In what situation/context would I use them?!
  • Denial: Why not just have a lovely file full of lovely methods/functions instead? I don't need them!
  • Anger: Guh! Another class! Why do I need this complicated nonsense that I don't understand!
  • Bargaining: Maybe if I use enough well-written efficient methods, i'll never need classes!
  • Depression: Urgh.. guess i'll have to learn to use classes someday...
  • Testing: This didn't work.. this didn't work.. hmm intriguing..
  • Acceptance: Everything is an object. Everything is a class. They're beautiful.
 On-queue nerd out.

Yes, believe it or not, after years of trying to avoid them, I finally love classes.
The thing about a class is that I've never understood all of its true values that it brings to a piece of code.
The perspective it brings helps you to think about your code truly as an object.

The first thing people tell you with such confidence is that a class is an 'object', and in your head you're sitting there thinking "okaay..."

"Think of it like a T-rex" they say, "where it has variables like 'tail' and arm_length'."

Aaand you'll still be none-the-wiser, because whilst you now know that variables can belong to a 'class', you don't know how to apply this abstract word called 'class' to your code, how they help you or when/where/how to use them.

Someone else may mention "They help you to organise your code." Which - in parts - is very true, but it still does not help you to understand when to actually implement them in your code.

The thing about coding is that it needs to be clear to read and easy to understand. The vast majority of your code that you write will either be read by someone else, or future you who has completely forgotten what it was that you wrote. So, it needs to be: Clear, Fast & Easy to read.

If you have never used dictionaries - you should. I would highly recommend to learn them before learning classes, because they'll only take you 5 mins to learn and will help you understand classes quicker and easier.

In python, a dictionary's syntax is this:

my_dict = {key : value}

The key can be anything, from a string called 'pinapple', to the float 725.23
The value can be anything, from the integer 3 to the string 'delicious'.

my_dict = {"shangrila": 17}
Anything, really.

The important concept here though, is that one value is linked with another, 'soul-bonded' if you will.

cheese_shop = { "cheddar_in_stock": 7, "wenslydale_in_stock":3}
print cheese_shop["cheddar_in_stock"]
returns: 7

This is similar to using zip( ) with two lists, except that it's much nicer to read it by using a dictionary in lots of cases like this. Damn ain't that swell?

Now, wouldn't it be nice if we didn't have to write our variable:

with square brackets and quotation marks? I mean we are lazy after all and want to write less for more.. right?

Wouldn't it be nice if we could instead write:


"Aah, no brackets! No nasty quotation marks" I heard you cry! Yes, that's right ladies and gentlemen, a whole 4 characters of you typing less thanks to classes.

It's kind of like a dictionary, in that it links two variables together. But it's like a beautiful swiss-army knife dictionary, because instead of the 'variable' you want to refer to being a string in square-brackets:

it's actually a variable:


 A.) Isn't it waay nicer to read:
       cheese_shop.cheddar_in_stock    than    cheese_shop["cheddar_in_stock"]