singleton ... again
Steven D'Aprano
steve at pearwood.info
Wed Feb 12 21:58:46 EST 2014
On Wed, 12 Feb 2014 23:04:32 +1300, Gregory Ewing wrote:
> Roy Smith wrote:
>> It looks to me like he's trying to implement a classic Gang of Four
>> singleton pattern.
>
> Which I've never really seen the point of in Python, or any other
> language for that matter. Just create one instance of the class during
> initialisation, put it in a global somewhere, and use it thereafter.
>
> If you really want to make sure nobody creates another instance by
> accident, delete the class out of the namespace after instantiating it.
That does not work. It is trivial to get the type from an instance:
py> class Singleton:
... pass
...
py> S = Singleton()
py> del Singleton
py> T = type(S)()
py> S
<__main__.Singleton object at 0xb71de9ec>
py> T
<__main__.Singleton object at 0xb71dea2c>
This is not aimed at the original poster, just a general observation. The
Singleton design pattern is overused because it is probably the only
design pattern that most programmers really understand.
--
Steven
More information about the Python-list
mailing list