Python singleton / object instantiation
python, singleton
Solution
Assigning to an argument or any other local variable (barename) cannot ever, possibly have ANY effect outside the function; that applies to your `self = whatever` as it would to ANY other assignment to a (barename) argument or other local variable.
Rather, override `__new__`:
class Singleton(object):
__instance = None
def __new__(cls):
if cls.__instance == None:
cls.__instance = object.__new__(cls)
cls.__instance.name = "The one"
return cls.__instance
I've done other enhancements here, such as uprooting the global, the old-style class, etc.
MUCH better is to use Borg (aka monostate) instead of your chosen Highlander (aka singleton), but that's a different issue from the one you're asking about;-).
Problem
I'm learning Python and i've been trying to implement a Singleton-type class as a test. The code i have is as follows: ``` _Singleton__instance = None class Singleton: def __init__(self): global __instance if __instance == None: self.name = "The one" __instance = self else: self = __instance ``` This works in part but the self = __instance part seems to be failing. I've included some output from the interpretor to demonstrate (the code above is saved in singleton.py): ``` >>> import singleton >>> x = singleton.Singleton() >>> x.name 'The one' >>> singleton._Singleton__instance.name 'The one' >>> y = singleton.Singleton() >>> y.name Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: Singleton instance has no attribute 'name' >>> type(y) <type 'instance'> >>> dir(y) ['__doc__', '__init__', '__module__'] ``` Is it possible to do what i'm trying? If not is there another way of doing this? Any suggestions welcome. Cheers.