Modules and Packages

Within programming, a module is a piece of software that has been designed to fit a specific function. For example, when creating a video game there may be one module for generating the maps, one for the game logic, one for the drawing of the game on the screen. Each module is a different file which are edited seperately from each other.

Writing Module
Modules in Python are just Python files with a .py extension. The name of the module is whatever the file is named. A module can have a set of functions, classes, variables and loops. These are examples of modules we could have:

mygame/

mygame/game.py

mygame/draw.py

The game.py will implement the game and handle the things that occur offscreen whilst the graw.py will use the draw_game function which will implement the logic for drawing the game on screen.

Modules can be imported by using the import command.

import draw

The "draw" module may have its contents looking as such:

def draw_game:

..... code

def clear_screen(screen)

.... code

Importing module objects to the current namespace
We can also use the from command to import functions directly into the main script's namespace. Example:

from draw import draw_game

This allows us to directly call a function within a module without needing to identify the name of the module

Importing all objects from a module
We can also use the import * ( * = ALL ) command to import all objects from within a module like so:

from draw import *

Writing packages
Packages are namespaces which contain multiple packages and modules within themselves. They are like directories.. in a way.

Each package in Python is a directory which ALWAYS contains a special file within it called __init__.py. It's allowed to be empty and is just a way for Python to recognise that it is a directory that contains a Python package, in order to allow importing.

ALL DONE! For basic level...