开发者

What is wrong with this singleton?

Currently I'm using a rather simplistic implementation of a singleton. However, I've never seen anything like this suggested on the web, which leads me to believe there might be something wrong with it...

class Singleton:
    def __init__():
        raise ...

    @staticmethod
    def some():
        pass

    @staticmethod
    def another():
        pass

Are there a开发者_运维知识库ny disadvantages to this implementation of a singleton (making all class members static). It is somewhat similar to using modules as singletons, except you wrap everything in a class.


Edit: I know about other ways to implement singletons in Python. What I don't like about them is that none of them are explicit (which goes against the Python zen):

Since I do a = Class() instead of something like a = Class.Instance(), it is not obvious that I'm dealing with on object with shared state (see note #1). If all members are static I at least have Class.someMethod() which kinda sorta suggests it's a singleton. What I don't like about this approach is that you can't use constructors and destructors, which removes the major advantage that singletons have over free functions, which is what you can do when they are created and destroyed (see note #2).

Note #1: I know I shouldn't care about the singleton's state (if I do, then it shouldn't be a singleton in the first place). I still want it to be explicit about what kind of class it is.

Note #2: When you create a singleton you could do some instantiation in its constructor. For example, in a singleton that deals with the graphics library, you could initialize the library in the constrctor. This way instantiation and deinstantiation happen automatically in the sinleton's constructors and destructor.

Or in a ResourceManager: the destructor could check if upon its destruction there still are resources in memory and act accordingly.

If you use free functions instead of singletons you have to do all of this by hand.


Yeah, it's a singleton, that's what's wrong with it. If you're going to make all methods static, don't bother making a class at all, just use free functions.


That's not a singleton. It's a stateless global collection of methods, wrapped in a type.

Nothing inherently wrong with it, unless you believe that it is a singleton.

Singleton implies one instance only.


It's impossible to mock, so it's hard to test.

That's what's wrong to it.

Actually having 1 object of a testable class is considered better practice.


We are borg! This way state is shared across every instance of the object.

class Borg(object):
    state = {}
    def __init__(self):
        self.__dict__ = self.state

a = Borg()
a.a = 34
b = Borg()
print b.a
c = Borg()
c.c = "haha"
print a.c

Just to quote the original


A singleton is a class which creates the same instance each time the constructor is called. Your class has no constructor, so it can not be a singleton.

The borg pattern is something different, here each instance shares the same attributes. Alex Martelli, the inventor of the borg pattern, gave a talk at the 2011 Europython where he told that Guido van Russom dislikes the borg pattern, but he told not why.

A real singleton in python has two advantages:

  1. you can subclass from it
  2. if you refactor your code and decide that you want different instances, the code change is minimized.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜