• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Data Dependence

Data Dependence

Learn about software development. Covering topics such as Coding in Python and JavaScript, developer productivity and more.

  • Home
  • Top Resources
  • Who Are You?
  • Show Search
Hide Search
You are here: Home / Programming / How to Logically Group Your Python Code into Modules

How to Logically Group Your Python Code into Modules

· · Leave a Comment

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.

Don't forget to share and follow!

Remember, don't forget to share this post so that other people can see it too! Also, make sure you subscribe to this blog's mailing list and follow me on Twitter so that you don't miss out on any useful posts!

I read all comments, so if you have something to say, something to share or questions and the like, leave a comment below!

‘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!

Twitter is the best place for a chat.

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Pocket (Opens in new window)

Related

Programming modules, python, structure

Primary Sidebar

LEVEL UP with the Data Dependence Newsletter! Subscribe!


Top Posts & Pages

  • How to Find Unclosed Tags and Brackets Using a Stack
  • A Quick Guide to Slicing in Python - Become a Python Ninja
  • Creating Attractive and Informative Map Visualisations in Python with Basemap
  • Mocking in Python - How to Bypass Expensive and External Code Within Your Tests
  • How to Get Better at Software Development Time Estimation

Data Dependence

Copyright © 2023 · Monochrome Pro on Genesis Framework · WordPress · Log in

  • Home
  • Top Resources
  • Who Are You?