SIngleton from __defaults__

Chris Angelico rosuav at gmail.com
Wed Jan 22 11:18:57 EST 2014


On Thu, Jan 23, 2014 at 3:07 AM, Asaf Las <roegltd at 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



More information about the Python-list mailing list