有没有简单优雅的方法来定义单例? [重复]

ぐ巨炮叔叔 提交于 2020-01-30 16:56:15
这个问题已经在这里有了答案 :
Python中创建单例 (22个答案)
去年关闭。

似乎有很多方法可以在Python中定义单例 。 关于堆栈溢出是否有共识?


#1楼

Python文档确实涵盖了这一点:

class Singleton(object):
    def __new__(cls, *args, **kwds):
        it = cls.__dict__.get("__it__")
        if it is not None:
            return it
        cls.__it__ = it = object.__new__(cls)
        it.init(*args, **kwds)
        return it
    def init(self, *args, **kwds):
        pass

我可能会重写它,使其看起来像这样:

class Singleton(object):
    """Use to create a singleton"""
    def __new__(cls, *args, **kwds):
        """
        >>> s = Singleton()
        >>> p = Singleton()
        >>> id(s) == id(p)
        True
        """
        self = "__self__"
        if not hasattr(cls, self):
            instance = object.__new__(cls)
            instance.init(*args, **kwds)
            setattr(cls, self, instance)
        return getattr(cls, self)

    def init(self, *args, **kwds):
        pass

扩展它应该相对干净:

class Bus(Singleton):
    def init(self, label=None, *args, **kwds):
        self.label = label
        self.channels = [Channel("system"), Channel("app")]
        ...

#2楼

class Singeltone(type):
    instances = dict()

    def __call__(cls, *args, **kwargs):
        if cls.__name__ not in Singeltone.instances:            
            Singeltone.instances[cls.__name__] = type.__call__(cls, *args, **kwargs)
        return Singeltone.instances[cls.__name__]


class Test(object):
    __metaclass__ = Singeltone


inst0 = Test()
inst1 = Test()
print(id(inst1) == id(inst0))

#3楼

正如公认的答案所说,最惯用的方法是只使用模块

考虑到这一点,这里有一个概念证明:

def singleton(cls):
    obj = cls()
    # Always return the same object
    cls.__new__ = staticmethod(lambda cls: obj)
    # Disable __init__
    try:
        del cls.__init__
    except AttributeError:
        pass
    return cls

有关__new__更多详细信息,请参见Python数据模型

例:

@singleton
class Duck(object):
    pass

if Duck() is Duck():
    print "It works!"
else:
    print "It doesn't work!"

笔记:

  1. 为此,您必须使用新型类(从object派生)。

  2. 单例在定义时进行初始化,而不是在首次使用时进行初始化。

  3. 这只是一个玩具例子。 我从未在生产代码中实际使用过它,也不打算这样做。


#4楼

我对此不太确定,但是我的项目使用“约定单例”(非强制性单例),也就是说,如果我有一个名为DataController的类,则可以在同一模块中定义它:

_data_controller = None
def GetDataController():
    global _data_controller
    if _data_controller is None:
        _data_controller = DataController()
    return _data_controller

它不够优雅,因为它有完整的六行。 但是我所有的单例都使用这种模式,并且至少非常显式(这是pythonic的)。


#5楼

辛格尔顿的同父异母兄弟

我完全同意staale的观点,在此留下创建单身同父异母兄弟的示例:

class void:pass
a = void();
a.__class__ = Singleton

a将作为同一类单身,即使它看起来并不像它的现在报告。 因此,使用复杂类的单例最终取决于我们对它们的了解不多。

这样,我们可以具有相同的效果,并使用更简单的东西,例如变量或模块。 不过,如果我们想使用类来保持清晰,并且因为在Python中类是一个object ,那么我们已经有了该对象(不是and instance,但它会像这样)。

class Singleton:
    def __new__(cls): raise AssertionError # Singletons can't have instances

如果我们尝试创建实例,就会有一个很好的断言错误,并且可以存储派生静态成员并在运行时对其进行更改(我喜欢Python)。 这个对象和其他同父异母的兄弟一样好(如果需要,您仍然可以创建它们),但是由于简单性,它往往会运行得更快。


#6楼

您可以像这样重写__new__方法:

class Singleton(object):
    _instance = None
    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super(Singleton, cls).__new__(
                                cls, *args, **kwargs)
        return cls._instance


if __name__ == '__main__':
    s1 = Singleton()
    s2 = Singleton()
    if (id(s1) == id(s2)):
        print "Same"
    else:
        print "Different"

#7楼

class Singleton(object[,...]):

    staticVar1 = None
    staticVar2 = None

    def __init__(self):
        if self.__class__.staticVar1==None :
            # create class instance variable for instantiation of class
            # assign class instance variable values to class static variables
        else:
            # assign class static variable values to class instance variables

#8楼

请参阅PEP318的此实现,使用装饰器实现单例模式:

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

@singleton
class MyClass:
    ...

#9楼

作为Python的新手,我不确定最常见的习惯用法是什么,但是我能想到的最简单的事情就是使用模块而不是类。 类上的实例方法原本只是模块中的函数,而任何数据都只是模块中的变量,而不是类的成员。 我怀疑这是解决人们使用单例的问题类型的Python方法。

如果您真的想要单例类,那么在Google的“ Python单例”的首次点击中有一个合理的实现,特别是:

class Singleton:
    __single = None
    def __init__( self ):
        if Singleton.__single:
            raise Singleton.__single
        Singleton.__single = self

这似乎可以解决问题。


#10楼

我没有真正的需要,因为具有功能(而不是类)的模块可以很好地用作单例。 它的所有变量都将绑定到模块,该模块无论如何都无法重复实例化。

如果您确实希望使用类,则无法在Python中创建私有类或私有构造函数,因此,您只能通过使用API​​的约定来防止多重实例化。 我仍然将方法放在模块中,并将模块视为单例。


#11楼

以下是Peter Norvig的Python IAQ示例如何在Python中执行Singleton模式? (您应该使用浏览器的搜索功能来查找此问题,没有直接链接,抱歉)

Bruce Eckel在他的着作“ Thinking in Python”中也有另一个例子(同样没有直接链接到代码)


#12楼

在Python中实现单例的方式略有不同,这是Alex Martelli(Google员工,Python天才)的borg模式

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

因此,它们不是共享所有实例具有相同的标识,而是共享状态。


#13楼

模块方法效果很好。 如果我绝对需要一个单例,那么我更喜欢使用元类方法。

class Singleton(type):
    def __init__(cls, name, bases, dict):
        super(Singleton, cls).__init__(name, bases, dict)
        cls.instance = None 

    def __call__(cls,*args,**kw):
        if cls.instance is None:
            cls.instance = super(Singleton, cls).__call__(*args, **kw)
        return cls.instance

class MyClass(object):
    __metaclass__ = Singleton

#14楼

一次我用Python写单例时,我使用了一个类,其中所有成员函数都具有classmethod装饰器。

class foo:
  x = 1

  @classmethod
  def increment(cls, y = 1):
    cls.x += y

#15楼

使用 ActiveState由Python实现的Singleton模式

似乎诀窍是将本应仅具有一个实例的类放在另一个类中。


#16楼

Google测试博客上也有一些有趣的文章,讨论了为什么单例是/可能是不好的并且是反模式的:


#17楼

我认为强迫一个类或实例成为单例是过分的。 我个人喜欢定义一个普通的可实例化类,一个半私有引用和一个简单的工厂函数。

class NothingSpecial:
    pass

_the_one_and_only = None

def TheOneAndOnly():
    global _the_one_and_only
    if not _the_one_and_only:
        _the_one_and_only = NothingSpecial()
    return _the_one_and_only

或者,如果在首次导入模块时实例化没有问题:

class NothingSpecial:
    pass

THE_ONE_AND_ONLY = NothingSpecial()

这样,您可以针对新实例编写测试,而不会产生副作用,也不需要在模块中添加全局语句,并且如果需要,您可以在将来派生变体。


#18楼

这是我自己的单例实现。 您所要做的就是装饰教室。 要获取单例,则必须使用Instance方法。 这是一个例子:

@Singleton
class Foo:
   def __init__(self):
       print 'Foo created'

f = Foo() # Error, this isn't how you get the instance of a singleton

f = Foo.instance() # Good. Being explicit is in line with the Python Zen
g = Foo.instance() # Returns already created instance

print f is g # True

这是代码:

class Singleton:
    """
    A non-thread-safe helper class to ease implementing singletons.
    This should be used as a decorator -- not a metaclass -- to the
    class that should be a singleton.

    The decorated class can define one `__init__` function that
    takes only the `self` argument. Also, the decorated class cannot be
    inherited from. Other than that, there are no restrictions that apply
    to the decorated class.

    To get the singleton instance, use the `instance` method. Trying
    to use `__call__` will result in a `TypeError` being raised.

    """

    def __init__(self, decorated):
        self._decorated = decorated

    def instance(self):
        """
        Returns the singleton instance. Upon its first call, it creates a
        new instance of the decorated class and calls its `__init__` method.
        On all subsequent calls, the already created instance is returned.

        """
        try:
            return self._instance
        except AttributeError:
            self._instance = self._decorated()
            return self._instance

    def __call__(self):
        raise TypeError('Singletons must be accessed through `instance()`.')

    def __instancecheck__(self, inst):
        return isinstance(inst, self._decorated)

#19楼

如果您不希望上面的基于元类的解决方案,并且不喜欢基于简单函数装饰器的方法(例如,因为在那种情况下,单例类上的静态方法将不起作用),则可以采用以下折衷方法:

class singleton(object):
  """Singleton decorator."""

  def __init__(self, cls):
      self.__dict__['cls'] = cls

  instances = {}

  def __call__(self):
      if self.cls not in self.instances:
          self.instances[self.cls] = self.cls()
      return self.instances[self.cls]

  def __getattr__(self, attr):
      return getattr(self.__dict__['cls'], attr)

  def __setattr__(self, attr, value):
      return setattr(self.__dict__['cls'], attr, value)

#20楼

我的简单解决方案基于函数参数的默认值。

def getSystemContext(contextObjList=[]):
    if len( contextObjList ) == 0:
        contextObjList.append( Context() )
        pass
    return contextObjList[0]

class Context(object):
    # Anything you want here

#21楼

如果您要装饰(注释)以后的类,则创建单例装饰器(也称为注释)是一种优雅的方法。 然后,您只需在类定义之前添加@singleton。

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

@singleton
class MyClass:
    ...

#22楼

好吧,我知道单身人士可能是好是坏。 这是我的实现,我只是简单地扩展了一种经典方法,即在内部引入高速缓存,并生成许多不同类型的实例,或者许多具有相同类型但具有不同参数的实例。

我将其称为Singleton_group,因为它将相似的实例组合在一起,并防止了可以创建具有相同参数的相同类的对象:

# Peppelinux's cached singleton
class Singleton_group(object):
    __instances_args_dict = {}
    def __new__(cls, *args, **kwargs):
        if not cls.__instances_args_dict.get((cls.__name__, args, str(kwargs))):
            cls.__instances_args_dict[(cls.__name__, args, str(kwargs))] = super(Singleton_group, cls).__new__(cls, *args, **kwargs)
        return cls.__instances_args_dict.get((cls.__name__, args, str(kwargs)))


# It's a dummy real world use example:
class test(Singleton_group):
    def __init__(self, salute):
        self.salute = salute

a = test('bye')
b = test('hi')
c = test('bye')
d = test('hi')
e = test('goodbye')
f = test('goodbye')

id(a)
3070148780L

id(b)
3070148908L

id(c)
3070148780L

b == d
True


b._Singleton_group__instances_args_dict

{('test', ('bye',), '{}'): <__main__.test object at 0xb6fec0ac>,
 ('test', ('goodbye',), '{}'): <__main__.test object at 0xb6fec32c>,
 ('test', ('hi',), '{}'): <__main__.test object at 0xb6fec12c>}

每个对象都携带单例缓存...这可能是邪恶的,但对某些对象来说却非常有用:)

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