Organizing module content
Inside any one module, we can specify variables, classes, or functions. They can be a handy way to store the global state without namespace conflicts. For example, we have been importing the Database
class into various modules and then instantiating it, but it might make more sense to have only one database
object globally available from the database
module. The database
module might look like this:
class Database: # the database implementation pass database = Database()
Then we can use any of the import methods we've discussed to access the database
object, for example:
from ecommerce.database import database
A problem with the preceding class is that the database
object is created immediately when the module is first imported, which is usually when the program starts up. This isn't always ideal, since connecting to a database can take a while, slowing down startup, or the database connection information may not yet be available. We could delay creating...