Packages and Modules
To clearly organize large projects into logical units, Python structures code into modules and packages.
A module is the base unit of Python’s program structure. A module is a file with a .py extension that contains all the functions, classes and definitions pertaining to that module.
A package is a collection of modules all saved inside a package folder. The package folder must contain a file called
__init__.py for Python to be able to identify the folder as a package.
Let’s have a look at the Django project we created in the last chapter to see packages and modules in action:
\mfdw_site init .py # This tells Python that # mfdw_site is a package. settings.py # The settings module for our project. urls.py # The urls module for our project. # ...
The Dot Operator
Dot notation is a common idiom in object-oriented programming. I like to think of the dot like a point because the dot points to something. In the case of Python, the dot operator points to the next object in the object chain. In Django classes, the object chain is like this:
Or in the case of functions:
Some real-life examples:
forms.Formpoints to the
Formclass in the
pages.apps.PagesConfigpoints to the
PagesConfigclass in the
appssub-package of the
pagespackage. I.e., the
apps.pyfile in you
django.conf.urlspoints to the
urlspackage inside the
djangowhich is also a Python package!
This can sometimes get a bit confusing, but if you remember to join the dots (sorry, bad pun there), you can usually find out what the dot operator is referring to.