Created
August 17, 2023 09:36
-
-
Save vub/2bc6c30b5dcf7828dce987f72de27183 to your computer and use it in GitHub Desktop.
Python - splitting a module into multiple files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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