Skip to content

Instantly share code, notes, and snippets.

@vub
Created August 17, 2023 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vub/2bc6c30b5dcf7828dce987f72de27183 to your computer and use it in GitHub Desktop.
Save vub/2bc6c30b5dcf7828dce987f72de27183 to your computer and use it in GitHub Desktop.
Python - splitting a module into multiple files
Python Clean Code Tip:
When your module becomes too big you can restructure it to a package while keeping all the imports from the module as they were.
πŸ‘‡
# BEFORE
# models.py
class Order:
pass
class Shipment:
pass
# └── models.py
# AFTER
# change to package
# models/__init__.py
from .order import Order
from .shipment import Shipment
__all__ = ["Order", "Shipment"]
# models/order.py
class Order:
pass
# models/shipment.py
class Shipment:
pass
# └── models
# β”œβ”€β”€ __init__.py
# β”œβ”€β”€ order.py
# └── shipment.py
# imports from module/package can stay the same
from models import Order, Shipment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment