def singleton_per_scope ( _cls , _scope = None , _renew = False , * args , * * kwargs ) : result = None singletons = SINGLETONS_PER_SCOPES . setdefault ( _scope , { } ) if _renew or _cls not in singletons : singletons [ _cls ] = _cls ( * args , * * kwargs ) result = singletons [ _cls ] return result
python singleton lazy initialize
Instanciate a singleton per scope .
def singleton_per_scope(_cls, _scope=None, _renew=False, *args, **kwargs):
    """Instanciate a singleton per scope."""

    result = None

    singletons = SINGLETONS_PER_SCOPES.setdefault(_scope, {})

    if _renew or _cls not in singletons:
        singletons[_cls] = _cls(*args, **kwargs)

    result = singletons[_cls]

    return result