8 Modules and packages
Functional programming, which we explored in the previous chapter, is one of the knottiest topics you’ll encounter in the programming world. I’m happy to tell you that this chapter, about Python’s modules, will provide a stark contrast, and will be one of the easiest in this book. Modules are important, but they’re also very straightforward to create and use. So if you find yourself reading this chapter and thinking, “Hey, that’s pretty obvious,” well, that’s just fine.
What are modules in Python, and how do they help us? I’ve already mentioned the acronym DRY, short for “Don’t repeat yourself,” several times in this book. As programmers, we aim to “DRY up” our code by taking identical sections of code and using them multiple times. Doing so makes it easier to understand, manage, and maintain our code. We can also more easily test such code.
When we have repeated code in a single program, we can DRY it up by writing a function and then calling that function repeatedly. But what if we have repeated code that’s used across multiple programs? We can then create a library—or, as it’s known in the world of Python, a module.