singleton in python ?

Marcel L.K. Opsteegh mlk_opsteegh at yahoo.com
Fri May 10 09:24:25 EDT 2002


"fritz steindl \(-:fs\)" <fs at floSoft.net> wrote in message news:<1020669846.963494 at newsmaster-04.atnet.at>...
> hi,
> 
> what is the best way to implement a singleton in python ???
> 
> ( class that allows only _one_ instantiation - like java singletons)
> 
> i miss static variables and functions in python (or i don't see them :-)
> 
> <thx>
> 
> fritz
> (-:fs)

I implement a sigleton normally as following:

class DatabaseManager:
    # Singleton (recycle connections)
    theConnectionPool = None
    class __ConnectionPool:
        def __init__(self):
            ...
					
        def getCursor(self, aRecycleFlag = CURSOR_RECYCLE):
            ...

    def __init__(self):
        if DatabaseManager.theConnectionPool == None:
            DatabaseManager.theConnectionPool = DatabaseManager.__ConnectionPool()
        else:
            pass

    def __getattr__(self, aName):
        return getattr(self.theConnectionPool, aName)

Regards,
Marcel.



More information about the Python-list mailing list