issue with singleton python call two times __init__

瘦欲@ 提交于 2019-11-29 11:23:00

Here's a slightly simpler way to write a Singleton:

class Singleton(object):
    __instance = None
    def __new__(cls):
        if cls.__instance is None:
            cls.__instance = super(Singleton,cls).__new__(cls)
            cls.__instance.__initialized = False
        return cls.__instance

    def __init__(self):      
        if(self.__initialized): return
        self.__initialized = True
        print ("INIT")

a = Singleton()
b = Singleton()
print (a is b)

although there may be better ways. I have to admit that I've never been fond of singletons. I much prefer a factory type approach:

class Foo(object):
    pass

def foo_singleton_factory(_singlton = Foo()):
    return _singleton

a = foo_singleton_factory()
b = foo_singleton_factory()
print (a is b)

This has the advantage that you can keep getting the same instance of Foo if you want it, but you're not limited to a single instance if you decide 10 years down the road that you don't want a true singleton.

PEP 318 has an example of a singleton decorator for classes:

def singleton(cls):
    instances = {}
    def getinstance():
        if cls not in instances:
            instances[cls] = cls()
        return instances[cls]
    return getinstance

@singleton
class MyClass:
    ...

(though I haven't used it myself.)

Btw, about...

I made a singleton like this

Also, you should mention that you copied it directly from ActiveState.

Since we're all ignoring your question and instead proposing alternative singleton implementations, I'll pitch in with my favourite. It takes advantage of the fact that a python module is only loaded once, no matter how many times you import it.

It's also based on the python motto "we're all consenting adults here" since, if you REALLY want to, you can instantiate it multiple times... but then you really have to put in an extra effort to do it wrong.

So in mysingleton.py:

class SingletonClass(object):
    def __init__(self):
        # There's absolutely nothing special about this class
        # Nothing to see here, move along
        pass

# Defying PEP8 by capitalizing name
# This is to point out that this instance is a Singleton
Singleton = SingletonClass()

# Make it a little bit harder to use this module the wrong way
del SingletonClass

Then use it like this:

from mysingleton import Singleton

# Use it!

I said you had to put in an extra effort to do things wrong. Here's how you could create two instances of the singleton class, making it a singleton no more:

another_instance = Singleton.__class__()

So how do you avoid this problem? I'll quote the doctor: don't do that then!


NOTE: This was added after the comments below were made

While I'm at it, here's another singleton variant that minimizes the amount of complicated code. It uses metaclasses:

class SingletonMeta(type):
    # All singleton methods go in the metaclass
    def a_method(cls):
        return cls.attribute

    # Special methods work too!
    def __contains__(cls, item):
        return item in cls.a_list

class Singleton(object):
    __metaclass__ = SingletonMeta
    attribute = "All attributes are class attributes"

    # Just put initialization code directly into the class
    a_list = []
    for i in range(0, 100, 3):
        a_list.append(i)

print Singleton.a_method()
print 3 in Singleton

In python 3 you'd create the singleton instance like this instead:

class Singleton(metaclass=SingletonMeta):
    attribute = "One... two... five!"

Now this one is a little more iffy, since the singleton is a class, and you can make instances of the singleton. In theory this is OK, since the singleton will still be a singleton even if it has instances, but you need to remember that Singleton() is not the singleton -- Singleton is! It might even suit your needs to have the singleton attributes readily available to its instances as class attributes.

Bakuriu

An other way:

>>> class Singleton(object):
...     def __new__(cls, *args, **kwargs):
...             try:
...                     return cls._instance
...             except AttributeError:
...                     val = cls._instance = object.__new__(cls, *args, **kwargs)
...                     return val
... 
>>> class A(Singleton): pass
... 
>>> a = A()
>>> a2 = A()
>>> a2 is a
True
>>> class B(Singleton): pass
... 
>>> b = B()
>>> b2 = B()
>>> b2 is b
True
>>> b is a
False
>>> class D(Singleton):
...     def __init__(self, v): self.v = v
... 
>>> d = D(1)
>>> d.v
1

If you are worried about multiple calls to __init__ then the choice is between using a decorator or a metaclass.

Overriding the __new__ method allow multiple __init__ calls because python always calls the __init__ method of an object returned by __new__ if the value returned is an instance of that class.

Anyway I think using a decorator it's the best thing, because it's probably the simpler solution.

If you want to know more ways of creating a singleton in python read this question.

By the way, if you want to have all instances with the same state(and not identity) then you might be interested in the Borg pattern. If you are unsure which one to choose, see this answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!