a Python package can have sub-packages and modules.
A directory must contain a file named
__init__.py
in order for Python to consider it as a package. This file can be left empty but we generally place the initialization code for that package in this file.
Here is an example. Suppose we are developing a game, one possible organization of packages and modules could be as shown in the figure below.
For example, if want to import the
start
module in the above example, it is done as follows.import Game.Level.start
Now if this module contains a function named
select_difficulty()
, we must use the full name to reference it.Game.Level.start.select_difficulty(2)
If this construct seems lengthy, we can import the module without the package prefix as follows.
from Game.Level import start
We can now call the function simply as follows.
start.select_difficulty(2)
No comments:
Post a Comment