Modules

When working with modules, it's important to remember that classes can be imported directly from the module, rather than importing the entire module. As you learned in Chapter 2Data Types and Modules, in the Types of imports section, import calls other libraries or modules, while from is a selective import, allowing you to get specific parts of a module; using from foo import * is dangerous, as it imports everything into the current namespace, which can cause shadowing of different objects, resulting in errors. Classes and functions are the tools that make importation work.

Since everything in Python is (from the interpreter's viewpoint) an object, everything can be imported. However, in practice, only classes and functions can be imported; if you have another program that is strictly in-line code, that is, no functions or classes, you'll just have to copy and paste it.

This is one reason why using classes or functions is important in your code. While you can make your code work without them, they improve the logic flow (since everything is segregated into its own logical block) and they make it possible to reuse code in different programs. Imagine trying to make a simulation program but you have to copy and paste code to screenshot out mathematical problems. It's easier to simply import the math library and utilize the classes available from it.