I am building what essentially could be viewed as a glorified database wrapper as a Python package, where I'm having several classes and functions spread out into different modules. My current problem is how to share the database connections with the different modules.
An example class in user.py:
class User(object): def __init__(self, user_name, db_con): self.user_name = user_name self._db_con = db_con
Which results in my __init__.py looking like this:
from .user import User as _User def _connect_dbs(): econ = _mdb.connect(host='db.com', user='user', passwd=pass, db='db', charset='utf8') return econ _db = _connect_dbs() def User(user_name): return _User(user_name, _db)
Is this a good solution, or could I implement it in another manner? While this avoids global variables, it does result in some code I would rather not have to write for quite a few functions.