
Hi folks. Earlier messages suggested a nice singleton decorator, which is shown in the draft PEP: def singleton(cls): return cls() class MyClass [singleton]: ... This has been mentioned as an argument against requiring or recommending that decorators accept and return callables. But i don't think this is a good way to write a singleton, because then the user of the class has to get instances by saying "MyClass" instead of "MyClass()". Better would be: def singleton(cls): instances = {} def getinstance(): if cls not in instances: instances[cls] = cls() return instances[cls] return getinstance Then, code that gets an instance makes more sense. MyClass() still instantiates MyClass -- it just always returns the same instance. I respectfully suggest that the PEP use the latter implementation in its singleton example instead. -- ?!ng

At 05:17 AM 3/30/04 -0600, Ka-Ping Yee wrote:
That's a stylistic decision. IMO, it's more pythonic to *not* call a constructor. One does not, after all, call modules in order to "construct" them, and modules are a prime example of singletons in Python. What benefit is there to forcing people to perform a call operation that doesn't *do* anything?
I respectfully suggest that the PEP use the latter implementation in its singleton example instead.
I respectfully disagree. :)

>> But i don't think this is a good way to write a singleton, because >> then the user of the class has to get instances by saying "MyClass" >> instead of "MyClass()". Phillip> That's a stylistic decision. IMO, it's more pythonic to *not* Phillip> call a constructor. One does not, after all, call modules in Phillip> order to "construct" them, and modules are a prime example of Phillip> singletons in Python. I think Ping's point is that if the user thinks Foo is a class, he's going to want to instantiate it like Foo(). Whether or not you get back a new instance or an existing one is sort of beside the point. It's simply a consistency issue. Skip

(Still catching up. I guess no help with the substance of the PEP will be forthcoming, so I'll try to get to things as I can.) ... [snip] ... Ping> But i don't think this is a good way to write a singleton, because Ping> then the user of the class has to get instances by saying Ping> "MyClass" instead of "MyClass()". Better would be: ... [snip] ... Ping> I respectfully suggest that the PEP use the latter implementation Ping> in its singleton example instead. Done. Skip

At 05:17 AM 3/30/04 -0600, Ka-Ping Yee wrote:
That's a stylistic decision. IMO, it's more pythonic to *not* call a constructor. One does not, after all, call modules in order to "construct" them, and modules are a prime example of singletons in Python. What benefit is there to forcing people to perform a call operation that doesn't *do* anything?
I respectfully suggest that the PEP use the latter implementation in its singleton example instead.
I respectfully disagree. :)

>> But i don't think this is a good way to write a singleton, because >> then the user of the class has to get instances by saying "MyClass" >> instead of "MyClass()". Phillip> That's a stylistic decision. IMO, it's more pythonic to *not* Phillip> call a constructor. One does not, after all, call modules in Phillip> order to "construct" them, and modules are a prime example of Phillip> singletons in Python. I think Ping's point is that if the user thinks Foo is a class, he's going to want to instantiate it like Foo(). Whether or not you get back a new instance or an existing one is sort of beside the point. It's simply a consistency issue. Skip

(Still catching up. I guess no help with the substance of the PEP will be forthcoming, so I'll try to get to things as I can.) ... [snip] ... Ping> But i don't think this is a good way to write a singleton, because Ping> then the user of the class has to get instances by saying Ping> "MyClass" instead of "MyClass()". Better would be: ... [snip] ... Ping> I respectfully suggest that the PEP use the latter implementation Ping> in its singleton example instead. Done. Skip
participants (3)
-
Ka-Ping Yee
-
Phillip J. Eby
-
Skip Montanaro