You Should Be Using Python Classes

You Should Be Using Python Classes

Python is cool. So are Python classes. If you develop with Python and haven’t used them yet, I suggest that you do. They’re something that I started using a few years ago and they have come in so handy when it comes to re-using code and maintaining scope easily.

When I first started learning about and using classes there were a few things that confused me and were different than just writing a simple script. You can’t -just- run them from the command line and expect them to work. You have to instantiate your class. That means you have to call it and it will create an empty object for you. So say you created a python class in zippy.py.

# zippy.py
class CoolClass:
    def hi(self):
        return 'Hi!'

# instantiate the class from the same file
coolClassObject = CoolClass()

The first thing is that your function is now inside a class. The second thing is you have this variable thing called self. So to instantiate this class you can call it within the same file or you can pull up a python shell and import it.

# pretend this is a python shell

import zippy
coolClassObject = zippy.CoolClass()

So now you’ve got a variable with your class object in it. Yay! Now I guess you want to call your hi function. Super straightforward. coolClassObject.hi() and there you go, you’ll get ‘Hi!’ returned.

The coolest thing about classes (In my opinion, but this is my blog, so what else would you expect?) is the __init__(): method. It’s basically a place where you can initialize all your variables, data passed in when you instantiate the class, etc. You can then use everything in your __init__(): throughout your class. I like being able to define static, global values or any data structures; if I have URLs that I need to reference for APIs I’ll also define them there. You can also pass in parameters when you first instantiate the class (required or optional). So so handy.

# zippy.py
class CoolClass:
    def __init__(yourName='Bort'):
        self.name = yourName
        self.group = 'Cool'
    def hi(self):
        msg = 'Hi, {0}. You are in the {1} group'.format(self.name, self.group)
        return msg

# instantiate the class from the same file with no parameter passed in
coolClassObjectDefault = CoolClass()

# instantiate the class from the same file with your name
coolClassObjectRachel = CoolClass('Rachel')

I’ve added the init method now and I’ve defined an optional variable that you can define when you first instantiate the class. If you’ve made changes in a python shell and you’re using python3 (which I’ll be using unless I say otherwise), you’ll need to import imp to reload the module.

When you run it now, you’ll get your message to return with the default argument of Bort for the first object and Rachel will be in the second one. Notice how I could instantiate two versions of the same class? Super handy.

I’ll wrap up with a brief talk about self. I like to think of it as mega-variable. Python officially calls it an instance variable. Anything that got defined in __init__(): is stored in self and you can then use it throughout the rest of the functions in your class. You just have to make sure that self is passed into each function (you’ll get an error otherwise) and then you can use it throughout your class functions.

-Rachel

Leave a comment