Python实现单例模式

定义

An implementation of the singleton pattern must:

  1. ensure that only one instance of the singleton class ever exists; and
  2. provide global access to that instance.

单例模式就是保证类在整个使用过程中有且只有一个实例.

Python原理

要想保证类的使用过程中有且只有一个实例, 就需要对Python中类的实例化, 以及实例化后的使用过程有所了解. 这个过程主要涉及到3个方法:

  1. __new__
  2. __init__
  3. __call__

这3个方法的调用过程是: 首先调用__new__, 来生成新的实例; 其次, 调用__init__对新实例进行相关设置; 最后, 如果类中实现了__call__方法的话, 那么类的实例也是一个可调用对象, 调用实例时instance()其实就是调用实例所属的类类的__call__方法.

清楚了3个方法的调用时机, 再结合单例模式的定义就不难实现Python版本的单例模式了.

Python实现

Python中实现单例模式主要有一下几种方式:

  1. __call__
  2. __new__

__call__

在python中一切都是对象, 类其实也是一个对象, 一个类是其元类(MetaClass)的实例(定义类的时候其实就会调用元类的__init__方法).

这样理解过来, 类的实例初始化实际上就是通过调用了类的元类的__call__方法实现的.

# coding: utf-8
"""
代码摘在mininet源代码
"""

class Singleton( type ):
    """Singleton pattern from Wikipedia
       See http://en.wikipedia.org/wiki/Singleton_Pattern
       Intended to be used as a __metaclass_ param, as shown for the class
       below."""

    def __new__( cls, *args, **kw ):
        return super( Singleton, cls ).__new__( cls, *args, **kw )
    
    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 MininetLogger( Logger, object ):
    """Mininet-specific logger
       Enable each mininet .py file to with one import:
       from mininet.log import [lg, info, error]
       ...get a default logger that doesn't require one newline per logging
       call.
       Inherit from object to ensure that we have at least one new-style base
       class, and can then use the __metaclass__ directive, to prevent this
       error:
       TypeError: Error when calling the metaclass bases
       a new-style class can't have only classic bases
       If Python2.5/logging/__init__.py defined Filterer as a new-style class,
       via Filterer( object ): rather than Filterer, we wouldn't need this.
       Use singleton pattern to ensure only one logger is ever created."""

    __metaclass__ = Singleton

    def __init__( self ):

        Logger.__init__( self, "mininet" )

        # create console handler
        ch = StreamHandlerNoNewline()
        # create formatter
        formatter = logging.Formatter( LOGMSGFORMAT )
        # add formatter to ch
        ch.setFormatter( formatter )
        # add ch to lg
        self.addHandler( ch )

        self.setLogLevel()

    def setLogLevel( self, levelname=None ):
        """Setup loglevel.
           Convenience function to support lowercase names.
           levelName: level name from LEVELS"""
        level = LOGLEVELDEFAULT
        if levelname is not None:
            if levelname not in LEVELS:
                raise Exception( 'unknown levelname seen in setLogLevel' )
            else:
                level = LEVELS.get( levelname, level )

        self.setLevel( level )
        self.handlers[ 0 ].setLevel( level )

    # pylint: disable=method-hidden
    # "An attribute inherited from mininet.log hide this method" (sic)
    # Not sure why this is occurring - this function definitely gets called.

    # See /usr/lib/python2.5/logging/__init__.py; modified from warning()
    def output( self, msg, *args, **kwargs ):
        """Log 'msg % args' with severity 'OUTPUT'.
           To pass exception information, use the keyword argument exc_info
           with a true value, e.g.
           logger.warning("Houston, we have a %s", "cli output", exc_info=1)
        """
        if self.manager.disable >= OUTPUT:
            return
        if self.isEnabledFor( OUTPUT ):
            self._log( OUTPUT, msg, args, kwargs )

    # pylint: enable=method-hidden

每次实例化MininetLogger时, 其实调用了Singleton的__call__方法, 这个可以在Singleton和MininetLogger类中相关方法添加打印日志进行验证.

当然Singleton还有其他的定义方式:

# https://stackoverflow.com/questions/6760685/creating-a-singleton-in-python
class Singleton(type):
    _instances = {}
    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

但是这种方式在多个类(A, B)同时指向Singleton为__metaclass__的话, 当实例化A后, 可以通过B._instances看到到A的instance.

__new__

每次实例化一个类的新实例的时候其实会调用类的__new__方法, 这样就可以通过在类的__new__方法中进行唯一实例的控制.

# https://stackoverflow.com/questions/6760685/creating-a-singleton-in-python

class Singleton(object):
    _instance = None
    def __new__(cls, *args, **kwargs):
        if not isinstance(cls._instance, cls):
            cls._instance = object.__new__(cls, *args, **kwargs)
        return cls._instance
        
class A(Singleton):
    pass
    
id(A()) == id(A())
# True

总结

单例模式就是保证在整个程序使用过程中无论实例化(class())多少次, 在内存中只保存一个实例. 在Python中通过利用__new____call__两个魔法函数就可以通过在类或者类的元类中定义这两个方法实现单例模式.

转载于:https://my.oschina.net/alazyer/blog/908709

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值