0

As I learn more about Python I am starting to get into the realm of classes. I have been reading on how to properly call a class and how to import the module or package.module but I was wondering if it is really needed to do this.

My question is this: Is it required to move your class to a separate module for a functional reason or is it solely for readability? I can perform all the same task using defined functions within my main module so what is the need for the class if any outside of readability?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79

2 Answers2

1

Modules are structuring tools that provide encapsulation. In other words, modules are structures that combine your logic and data into one compartment, in the module itself. When you code a module, you should be consistent. To make a module consistent you must define its purpose: does my module provide tools? What type of tools? String tools? Numericals tools...?

For example, let's assume you're coding a program that processes numbers. Typically, you would use the builtin math module, and for some specialized purposes you might need to code some functions and classes that process your numbers according to your needs. If you read the documentation of math builtin module, you'll find math defines classes ad functions that relate to math but no classes or functions that process strings for instance, this is cohesion--unifying the purpose of your module. Keep in mind, maximizing cohesion, minimizes coupling. That's, when you keep your module unified, you make it less likely to be dependent on other modules.

Is it required to move your Class to a separate module for a functional reason or is it solely for readability?

If that specific class doesn't relate to your module, then you're probably better off moving that class to another module. Definitely, This is not a valid statement all the time. Suppose you're coding a relatively small program and you don't really need to define a large number of tools that you'll use in your small program, coding your class in your main module doesn't hurt at all. In larger applications where you need to write dozens of tools on the other hand, it's better to split your program to modules with specified purposes, myStringTools, myMath, main and many other modules. Structuring your program with modules and packages enhances maintenance.

If you need to delve deeper read about Modular programming, it'll help you grasp the idea even better.

GIZ
  • 4,409
  • 1
  • 24
  • 43
  • So what you are saying is it is not "Required" however to prevent a maintenance nightmare for larger programs it is mostly a good idea to move `Classes` "Tools" into their own `module` "toolbox" for their intended jobs. – Mike - SMT Apr 27 '17 at 15:12
1

You can do as you please. If the code for your classes is short, putting them all in your main script is fine. If they're longish, then splitting them out into separate files is a useful organizing technique (that has the added benefit of the code in them no getting recompiled into byte-code everytime the the script they are used in is run.

Putting them in modules also encourages their reuse since they're no longer mixed in with a lot of other unrelated stuff.

Lastly, they may be useful because modules are esstentially singleton objects, meaning that there's only once instance of them in your program which is created the first time it's imported. Later imports in other modules will just reuse the existing instance. This can be a nice way to do initialize that only has to be done once.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • That makes sense. When I asked the question I was not 100% sure if putting a class in its own method was required for any reason or it was preference. Most of the content I have read about or watch related to python classes on YouTube have not maid it clear if it was required or not to have classes in a separate module. – Mike - SMT Apr 27 '17 at 15:18