• python singleton design pattern super() 多继承


    python  singleton design pattern 

    1.  decorate

    2.  baseclass

    3.  metaclass

    4.  import module

    5. super()

    一、A decorator

    def singleton(class_):
        instances = {}
        def getinstance(*args, **kwargs):
            if class_ not in instances:
                instances[class_] = class_(*args, **kwargs)
            return instances[class_]
        return getinstance
    
    @singleton
    class MyClass(BaseClass):
        pass

    当用MyClass() 去创建一个对象时这个对象将会是单例的。MyClass 本身已经是一个函数。不是一个类,所以你不能通过它来调用类的方法。所以对于

    m=MyClass() n = MyClass()  o=type(n)()   m==n and m!=o and n != o  将会是True

    二、baseclass

    class Singleton(object):
        _instance = None
        def __new__(class_, *args, **kwargs):
            if not isinstance(class_._instance, class_):
                # class_._instance = object.__new__(class_)   这行语句和下一行语句作用一样的
                class_._instance=super(Singleton,class_).__new__(class_)
            return class_._instance
    class MyClass(Singleton):
        def __init__(self,name):
            self.name = name
            print(name)

    pros  

      是真的类

    cons:

    在多继承的时候要注意

    三、metaclass

    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]
    
    #Python2
    class MyClass(BaseClass):
        __metaclass__ = Singleton
    
    #Python3
    class MyClass(BaseClass, metaclass=Singleton):
        pass

    Pros

    • It's a true class
    • Auto-magically covers inheritance
    • Uses __metaclass__ for its proper purpose (and made me aware of it)

    四、通过导入模块


    五、

    super(type[,object or type])

    If the second argument is omitted, the super object returned is unbound. If the second argument is an object, isinstance(obj, type) must be true.

    If the second argument is a type, issubclass(type2, type) must be true (this is useful for classmethods).

    note :super()  只能用于新式类

    链接 https://rhettinger.wordpress.com/2011/05/26/super-considered-super/

    多继承,在python3 中全部都是新式类

    新式类的继承顺序是广度优先,python2 中的经典类是深度优先

    通过一个例子来理解

    class A(object):
        def f1(self):
            print('a.f1')
    
    class B(A):
        def f2(self):
            print('b.f1')
        
    class F(object):
        def f1(self):
            print('f.f1')
    
    class C(B,F):
        def f3(self):
            print('c.f1')
    
    insta = C()
    insta.f1()

    关系图

            

     将会打印出a.f1

    如果代码改为

    class A(object):
        def f1(self):
            print('a.f1')
    
    class B(A):
        def f2(self):
            print('b.f1')
        
    class F(A):
        def f1(self):
            print('f.f1')
    
    class C(B,F):
        def f3(self):
            print('c.f1')
    
    insta = C()
    insta.f1()

     关系图如下:

     运行结果是f.f1


    python 2 代码如下

    class A:  #经典类
        def foo(self):
            print'A'
    class B(A):

        def foo(self):
            print'B'
    class C(A):
        pass
        #def foo(self):
         #   print'C'
    class D(B):
        #def foo(self):
         #   print 'D'
        pass

    class F(B):
        #pass
        def foo(self):
            print 'F'
            pass

    class G(D,F):
        pass
    g1=G()
    g1.foo() #打印出 B


    python 3 代码

    class A(object):
        def f1(self):
            print('a.f1')
    
    class B(A):
        pass
         def f1(self):
             print('b.f1')
        
    class C(A):
        def f1(self):
            print('c.f1')
    
    class D(B):
        pass
        # def f1(self):
        #     print('D.f1')
    
    class F(B):
        pass
        def f1(self):
            print('f.f1')
    
    class G(D,F):
        # def f1(self):
        #     print('g.f1')
        pass
        
    insta = G()
    insta.f1()  #打印出f.f1

  • 相关阅读:
    Java中Iterator类的详细介绍
    Java中Iterator类的详细介绍
    Java中Iterator类的详细介绍
    Java中Iterator类的详细介绍
    VC++的Unicode编程
    实现系统滚动条换肤功能
    透明位图的显示
    用于树控件消息处理的几个结构
    C++实现半透明按钮控件(PNG,GDI+)
    Bootstrap+Knockout.JS+ASP.Net MVC3+PetaPOCO实现CRUD操作
  • 原文地址:https://www.cnblogs.com/yuyang26/p/7717571.html
Copyright © 2020-2023  润新知