1.Modules
A module is just a `.py` file. `import mathy` runs mathy.py once and binds the name `mathy` to the resulting namespace object, so every name mathy.py defined is reachable as mathy.something.
Key features
- Any file ending in .py is automatically a module — nothing marks it as one
- import name runs name.py once and binds name to the resulting module object
- Access what the file defined as name.thing — an attribute lookup, not a copy
- import name as alias binds a shorter or non-clashing name to the same module
- from name import thing binds thing directly, skipping the name. prefix
Ways to bring a module — or a name from it — into scope
- mathy.py — PI = 3.14159, def area(r): ...
- import mathy — the file runs, top to bottom, once
- mathy — a namespace: mathy.PI, mathy.area
Command
import mathy # bind the module itself
import mathy as m # bind it under a shorter name
from mathy import area # bind one name directly
from mathy import area as circle_area # bind it, renamedimport runs a .py file once and hands back a namespace object — mathy.thing reaches into it; from mathy import thing skips the prefix.