Tips for Grouping Your Functions

Put functions and variables that logically belong together in the same module. If there isn’t some logical connection—for example, if one of the functions calculates how much carbon monoxide different kinds of cars produce, while another figures out bone strength given the bone’s diameter and density—then you shouldn’t put them in one module just because you happen to be the author of both.

Of course, people often have different opinions about what is logical and what isn’t. Take Python’s math module, for example; should functions to multiply matrices go in there too, or should they go in a separate linear algebra module? What about basic statistical functions? Going back to the previous paragraph, should a function that calculates gas mileage go in the same module as one that calculates carbon monoxide emissions? You can always find a reason why two functions should not be in the same module, but a thousand modules with one function each are going to be hard for people (including you) to work with.

As a rule of thumb, if a module has less than a handful of things in it, it’s probably too small, and if you can’t sum up the contents and purpose of a module in a one- or two-sentence docstring, it’s probably too large. These are just guidelines, though; in the end, you’ll have to decide based on how more experienced programmers have organized modules, like the ones in the Python standard library, and eventually on your own sense of style.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.197.10