Modules that semantically belong together can be stored in their own directory (or sub directories). This helps to recognize their relationship and reduces namespace cluttering when importing them.
Importing these files
Before doing anything, I store the names of the current local scope in the variable global_names
so that I can compare it with the names in the current local scope after executing the import
stateemnt:
>>> global_names = dir()
>>> print(global_names)
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
I now execute the an import
statement. In contrast to the previous examples, this time, the name tq84_dir
refers to a file system directory rather than a file name:
import tq84_dir
I perform a diff between the names that are stored in global_names
and those that are now present in the current local scope and store the result in new_names
:
>>> new_names = list(set(dir()) - set(global_names))
>>> print(new_names)
['tq84_dir', 'global_names']
It turns out that importing the tq84_dir
module (package) has created a corresponding new name (tq84_dir
) in the current local scope.
(
global_names
was created after the first call of
dir()
executed, hence it is also reported)
The type of tq84_dir
is module:
>>> print(type('tq84_dir'))
<class 'module'>
As before, I store the list of attributes that are present in the tq84_dir
module in a variable to be able to diff it later.
>>> tq84_names_init = dir(tq84_dir)
Although the directory contains two files (sub modules?), they are not yet loaded (imported):
>>> print(tq84_names_init)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
It's about time to import mod_one.py
and check how it influences the namespace of tq84_dir
:
>>> import tq84_dir.mod_one
>>> tq84_names_1 = dir(tq84_dir)
>>> new_names = list(set(tq84_names_1) - set(tq84_names_init))
>>> print(new_names)(
['mod_one']
Same thing with mod_two.py
:
>>> import tq84_dir.mod_two
>>> tq84_names_2 = dir(tq84_dir)
>>> new_names = list(set(tq84_names_2) - set(tq84_names_1))
>>> print(new_names)
['mod_two']
Finally, I call the functions that were defined in these package-modules:
>>> tq84_dir.mod_one.func()
I was defined in mod_one
>>> tq84_dir.mod_two.func()
I was defined in mod_two
These statements are also recorded
here.