Nested Modules

We have seen how Python scripts and programs can grow to a considerable size. In order to manage the complexity and keep our code clean and readable, we can structure our program using modules and packages. In Python, a module is simply a file containing Python code. Modules can import other modules, and this can lead to nested modules. Packages, on the other hand, are a way of organizing related modules into a directory hierarchy.
Consider that you are developing a game, and you have different functionalities such as sound, level, character, etc., which all are divided into their own modules. Now, the sound module itself has various components, for instance, effects, filters, echo, and so on. In such a scenario, you can create a nested module. Let's understand how to create it:
First, we create a directory (folder) for our package. For instance, we might call this game:
game/                     # The directory of our game package
Inside the game directory, we create the sound directory and an empty file. The file is required to make Python treat the directories as containing packages.
    sound/                # Subdirectory for the 'sound' module       # Makes Python treat 'sound' as a package
Now, we can create different Python files inside the sound directory for effects, filters, and echo. Each of these files represents a submodule of the sound module.
    sound/        # The 'effects' submodule        # The 'filters' submodule           # The 'echo' submodule
Let's say looks like this:
def echo_filter(soundwave):
    return soundwave + '...'   # Adds echo to the soundwave

def distort_filter(soundwave):
    return soundwave[::-1]     # Reverses the soundwave for distortion
If we want to use these functions in a different module within our game package, we can import them. We use a dot (.) to navigate through the package. Here's an example in a hypothetical file:
from game.sound.effects import echo_filter, distort_filter   # Importing the functions

soundwave = 'pew pew'
echoed = echo_filter(soundwave)
distorted = distort_filter(soundwave)

print(echoed, type(echoed))         # Prints 'pew pew...', <class 'str'>
print(distorted, type(distorted))   # Prints 'wep wep', <class 'str'>
Remember, organizing your Python code into modules and packages can help to keep it clean and readable, as well as make it easier to maintain and develop.

The Role of in Python Packages

In Python, the file serves a special purpose for directories that you intend to use as Python packages. Traditionally, an file was required for Python to recognize a directory as a package containing modules. However, in more recent versions of Python (3.3 and later), thanks to the introduction of implicit namespace packages, this is no longer strictly necessary.
But even with this change, files are far from obsolete and maintain their utility in certain situations. For instance, they can be used to execute package initialization code, control the imports with __all__ variable, or define convenience imports. Consider this directory structure:
In game/sound/ we could have:
from .effects import echo_filter
from .filters import distort_filter
from .echo import echo_sound
Here we are importing the submodules in the file. Now, the submodules effects, filters, and echo can be directly accessed when we import sound. So, in another part of the program, you could do:
from game.sound import echo_filter, distort_filter

soundwave = 'pew pew'
echoed = echo_filter(soundwave)
distorted = distort_filter(soundwave)
In this example, plays a crucial role in consolidating various submodules and providing a simplified interface for importing functionalities. While Python's newer versions do not enforce the presence of, using it this way can make your packages easier to use and manage.
To check your solution you need to sign in
Sign in to continue