Summary

In this chapter, we looked at the details of how Python modules and packages work. We saw that modules are simply Python source files that get imported using an import statement, and that packages are directories of Python source files identified by a package initialization file named __init__.py. We learned that packages can be defined inside other packages to form a tree-like structure of nested packages. We looked at how modules and packages can be initialized, and how the import statement can be used in various ways to import modules and packages, and their contents, into your programs.

We then saw how relative imports can be used to import modules relative to your current position in the package hierarchy and how the __all__ variable can be used to control what gets included in an import.

We then learned about circular dependencies and how to avoid them, and we finished by learning about chameleon modules, which can act as both importable modules and as standalone programs that can be run from the command line.

In the next chapter, we will apply what we have learned to the design and implementation of a more complicated program, and we will see how a good understanding of these techniques will let us build a system that is robust and can be updated to meet changing requirements.

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

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