This is going to be a short post, it’s just something that hit me today as I was looking over one of my current projects.
Python is an object orientated language, everything in Python is an object. However this is extremely misleading. Unlike languages such as Java, in Python you are not forced to use the OOP paradigm and indeed, using it is not always the right choice.
However recently I have noticed something about myself and how this has affected the way I design my programs; I don’t. Or at least, I don’t put as much thought into it. I originally come from a Java background so I am very used to designing things in an OOP manner and separating my programs’ logic up. However because in Python I’m not forced to do this, I got lazy.
After realising this today, I spent some time reorganising a project of mine. I realised that really, some of the logic in one of my modules should be extracted and placed in its own. After doing this, my code was less cluttered and made more sense.
After reading this post, you will have a better idea of how to divide your program up into modules too.
What actually is a Python module?
It’s just a file containing python code right? Well yes, but how do you decide what code should go into a module?
If you have a large project, there is no doubt that it should be split up into a collection of modules, but how? The answer, is into groups of functions that make logical sense. A fantastic example of this is the Requests library. The grouping makes sense; everying in cookies.py handles cookies and everything in sessions.py deals with sessions. It’s simple, navigable and avoids files becoming cluttered.
How should I divide my code up into modules?
There isn’t really a set way to do this as far as I know, but there are some patterns that I often see other developers using in their programs.
- Group code that deals with specific components or sections of your program. The Requests library that I talked about earlier on, with specific modules for things like cookies and sessions is a good example of this.
- Abstract out general utilities that are used by multiple modules into a utils module.
- If you have a GUI, have a module that deals with just that.
- If you have a database, have a module for connecting to and interacting with it.
If you look around other popular and reputable libraries, you can see many examples of these in use and more. Reading code by notably good programmers will help you get a feel for how to organise and divide your code.
Like I did, take a look over some of your past and current projects and really think about whether the way you’ve structured them is the best way or not. Your projects will end up looking much better, and hopefully after realising these little structural improvements, your future code will improve too.
That’s it for this post, I hope you managed to get some value out of it.
‘Ello, I’m Jamal – a Tokyo-based, indie-hacking, FinTech software developer with a dependence on data.
I write Shakespeare-grade code, nowadays mostly in Python and JavaScript and productivity runs in my veins.
I’m friendly, so feel free to say hello!