I have a web app in python and there is a main file that is a router.
The router reads the path and should:
- only now import the module needed for the request, or
- call the method directly. (In this case all modules needed by the app are imported at the beginning of the file)
In the first case:
router(path, args):
// now import the module needed and run it
In the second case the app would look like:
from services.a import a1
from services.a import a2
from services.a import a3
from services.a import a4
from services.a import a5
from services.a import a6
// and then in the router function
rooter(path, args):
// based on the path will call a fn
a1(args...)
What is better?
From PEP 8, “Style Guide for Python Code”:
Unless you have a good reason to violate this, follow it.