Late initialization using __getattribute__
Michele Simionato
michele.simionato at gmail.com
Wed Sep 3 23:26:37 EDT 2008
On Sep 4, 12:26 am, bukzor <workithar... at gmail.com> wrote:
> I'm trying to optimize my number of connections by not fully
> initializing (read: not connecting) my connection until it's used in
> some way.
I had the same use case and I solved with a simple property. Here is
the code
I have for pymssql:
@property
def conn(self):
if self._conn is None:
self._conn = _mssql.connect(self.host, self.user,self.passwd)
self._conn.select_db(self.dbname)
return self._conn
The connection is really instantiate only when you call self.conn to
perform the query, not when you instantiate the class.
More information about the Python-list
mailing list