Python/Classes & structure 6 min
Modules and structure
Split a growing script into importable pieces.
importmodulestructure
Every .py file is a module. import brings in a whole module, from ... import pulls specific names, and as renames them.
python · editable
loading editor…
As a script grows, separate the layers: loading data, transforming it, and reporting. Each becomes a module with small functions, which makes the pieces testable in isolation.
The `if __name__ == "__main__":` guard lets a file be both an importable module and a runnable script — the code under it only runs on direct execution.
python · editable
loading editor…
Keep import-time side effects out of modules. Importing a file should define things, not go and read a database.
practice this