28

Python has many modules (such as re) that perform a specific set of actions. You can call the functions of this module and get results, and the module as a whole has an idea behind it (in this case, dealing with regular expressions).

Classes seem to do almost the exact same thing, but they also seem to use properties quite a bit more than modules.

In what ways are modules different than classes? (I know I can't subclass a module, but is that it?) When should I use a class instead of a module?

2
  • Are you aware that when you call module methods, sometimes the module is just passing through to an instance of a class? See for example the random module. I don't recall whether this is true of re, however.
    – jscs
    Commented Aug 25, 2016 at 22:51
  • 2
    A related concept to know - Module vs. Package?
    – RBT
    Commented Jul 23, 2018 at 6:05

2 Answers 2

20

A python module is nothing but a package to encapsulate reusable code. Modules usually, but not always, reside in a folder with a __init__.py file inside of it. Modules can contain functions but also classes. Modules are imported using the import keyword.

Python has a way to put definitions in a file and use them in a script or in an interactive instance of the interpreter. Such a file is called a module; definitions from a module can be imported into other modules or into the main module.

Learn more about Python modules at these links:

https://docs.python.org/2/tutorial/modules.html (Python 2) https://docs.python.org/3/tutorial/modules.html (Python 3)

Classes, in the other hand, can be defined in your main application code or inside modules imported by your application. Classes are the code of Object Oriented Programming and can contain properties and methods.

Learn more about Python classes at these links:

https://docs.python.org/2/tutorial/classes.html (Python 2) https://docs.python.org/3/tutorial/classes.html (Python 3)

4
  • 4
    So it's not a bad thing to have a single class in a module, even if that class only has functions? I feel like in that situation I should generally just make a module without making a class. Is there an underlying rule as to when to use classes instead of modules? (That's what I'm trying to ask... Should I edit my question to make it more clear?)
    – Pro Q
    Commented Aug 25, 2016 at 23:48
  • 11
    You can create multiple instances of a class, but you cannot create instances of a module. You could compare modules to static classes or singletons. Commented Aug 26, 2016 at 2:24
  • 1
    Modules don't always reside in a folder with a __init__.py file. Commented Dec 13, 2018 at 12:12
  • 3
    @ProQ In languages like Python where you can have functions in modules rather than classes (as opposed to, say, Java) it's generally good form to just keep static functions at the module level. Classes imply instances with their own state.
    – Turksarama
    Commented Nov 1, 2019 at 7:02
6

If it's a bunch of pure functions, e.g. re, I put them in a module. I use a class when I know there's state I want to keep with that code (e.g. a file that gets read in once, but whose data is used by several of the functions).

Not the answer you're looking for? Browse other questions tagged or ask your own question.