Late initialization using __getattribute__

Michele Simionato michele.simionato at
Thu Sep 4 05:26:37 CEST 2008

On Sep 4, 12:26 am, bukzor <workithar... at> 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:

def conn(self):
    if self._conn is None:
         self._conn = _mssql.connect(, self.user,self.passwd)
    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