On Wednesday, January 22, 2014 6:18:57 PM UTC+2, Chris Angelico wrote: > On Thu, Jan 23, 2014 at 3:07 AM, Asaf Las <r...@gmail.com> wrote: > > 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] > > > > Why not simply: > def get_singleton(x = SomeClass()): > return x > Or even: > singleton = SomeClass() > ? Neither of the above provides anything above the last one, except > for late creation. > ChrisA
Hi Chris Does it make sense to use former as template to make singleton from any class as below, so instead of addressing your second proposal using module name we can directly call this one supplying class candidate for singleness as argument to function? class whatever(): def __init__(self): self.one = 1 self.zero = 0 def singleton_provider(someclass, x = [None]): if singleton_provider.__defaults__[0][0] == None: singleton_provider.__defaults__[0][0] = someclass() return singleton_provider.__defaults__[0][0] print(id(singleton_provider(whatever))) Thanks Asaf -- https://mail.python.org/mailman/listinfo/python-list