SIngleton from __defaults__

Asaf Las roegltd at gmail.com
Wed Jan 22 11:07:47 EST 2014


Hi 

Inspired by "Modifying the default argument of function"
https://groups.google.com/forum/#!topic/comp.lang.python/1xtFE6uScaI

is it possible to create singleton using construct below : 

def singleton_provider(x = [None]):
    if singleton_provider.__defaults__[0][0] == None:
        singleton_provider.__defaults__[0][0] = SomeClass()
    return singleton_provider.__defaults__[0][0]

and question - how to make it work in multithreaded app
when multiple threads are racing to create it first?

Thanks 

Asaf



More information about the Python-list mailing list