Since its introduction in the 1980’s, Python has risen to become one of the most popular and powerful coding languages of all time. Owing to its learnability, its flexibility, its thriving community, and its wide array of 3rd-party libraries, Python has come to be used for everything from backend web development to the highest reaches of machine learning.
In this piece, we’re going to discuss Python classes. Mastering classes is an important indication of the transition from being a beginning programmer to being an intermediate one. They will allow you to abstract your code into more powerful, general-purpose programs that solve a wider variety of problems.
Functions and Classes
In my view, the best way to understand classes is by first understanding functions, then using this as an analogy.
Consider the following extremely simple piece of code:
All this does is print the phrase ‘hello world’ to the screen. This is perfectly fine for the very first program you ever write, but observe that it doesn’t give us very many places to go. What if, from here, we want to iterate through the items in a list, printing each one?
This simple print statement would require us to be making constant manual changes to achieve our goals. That’s no way to code.
Instead, we could wrap this print statement in a function, giving it a certain measure of flexibility. In this example we take a list as an argument and print out each element in the list:
for element in msg_list:
So what exactly have we done? We’ve abstracted our problem, and used Python to create something that’s able to perform a function far more quickly than we could by sitting around hand-editing all day. Rather than thinking about how to write a program to print out a message, we’ve made one able to take a whole bunch of messages and print them out one at a time.
This is the source of the power of coding. There wouldn’t be much point if we were stuck with individual print statements. But with functions, classes, modules, and all the rest, we can do everything from make self-driving cars to invest in the stock market, just by giving computers carefully-written instructions.
And if a function is one level of abstraction above just writing a one-off piece of code, classes are a level of abstraction above that. What if instead of just one printing function, we wanted to gather several of them together into a single structure which captures all the relevant behavior?
It might look like this:
def print_a_message(self, msg_list):
for element in msg_list:
Now we are able to ‘instantiate’ this class in a variable and gain access to all of its functionality. In this way we can make as many copies of this code as we please, negating any need to duplicate our code-writing efforts.
For the purposes of this article it’s not that important for you to be able to follow the technical details of instantiation and writing classes. You just need to see the motivation for writing a class, which is to make progressively more abstract and powerful code.
Python Class Tutorials
If the above has you wanting to take a class class (see what I did there?), here are some great resources to get you going:
- The Python documentation. It’s hard to beat going directly to the official documentation. In my experience this can be a little hard to get through for a beginner, so consider pairing it with something one of these other resources.
- Learn Python. This interactive tutorial let’s learn about objects and classes by experimenting with code in your browser.
- Real Python. This tutorial focuses not only on classes but on object-oriented programming more generally. It’s a great way to learn about classes in the broader context of the Python approach to coding.
- Jeff Knupp. Another tutorial which goes into object-oriented programming, this tutorial does a great job of discussing inheritance, composition, and other features of classes that make them so powerful.
With that, you should be well on your way to Python mastery!