Review
- Method arguments and Class Attributes
- Understanding Modules and Packages
-
Modules
-
A module is a single namespace with many possible values (functions, classes, constants, etc.)
-
Usually corresponds to a file.
-
All modules have a name.
-
Python has only one type of module object.
-
A Package is just a module that may contain other modules (or packages).
-
It is module with a path attribute.
-
Packages organize modules into a named hierarchy.
-
Packages : modules :: directories : files
-
Usually corresponds to a directory with with an init.py file in it.
-
There are two kinds of packages (as of 3.3)
-
Regular packages are traditional (2 and up to 3.2), they are a directory with an init.py file. They are dependent on the file system.
-
Namespace packages are composed of portions. They do not have an init.py file. Portions can be anywhere. They are not dependent on the file system. They may be virtual.
-
Import statements bring modules into modules. Combines two operations:
-
Searches for named module.
-
Binds results to local name, i.e. the fully qualified path or an alias.
-
Compiles and runs the files and packages it imports.
-
Some general ideas
-
You can think of a module as a supplemental file relative to a top-level program or script.
-
Module files generally don’t do anything when run directly – they are used by other files.
-
When modules are imported, their elements are called attributes
-
Imports execute init.py files from the context of the directory of execution. Think of this as the project directory.
-
File and directory names should follow variable and function naming conventions since they will be used to create the names used in the importing program.
-
Global scope is always the file enclosing it, regardless of which module it is ultimately called from.