Introduction

I sometimes wonder when it is better to use a class, and it is more appropriate to use a module.

As far as I have so far understood, the simple answer is “state”. A class should be used for functionality that will require instantiation, or that needs to keep track of state.

A module can be used either as a way to mix functionality into multiple classes, or as a way to provide one-off features that don’t need to be instantiated or to keep track of state. A class method would also be used for the latter.

A class method seems more appropriate when you have an existing class that needs some singleton functionality. If what you’re making consists only of singleton methods, it makes more sense to implement it as a module and access it through the module directly.

A module can be used as a namespace for storing multiple objects (constants, methods, classes) when the module itself will not require state, that doesn’t mean that the objects within that class cannot themselves hold state.

Resources

Resource_1: Discussion on Stackoverflow

Resource_2: Answer on Stackoverflow

Resource_3: singleton methods and metaclasses