• 元类相关试题(单例)


    元类相关试题

    一、在元类中控制把自定义类的数据属性都变成大写

    class Mymeta(type):
        def __new__(cls, name,bases,dic):
            attr = {}
            for k,v in dic.items():
                if not k.startswith('__'):
                    k = k.upper()
                    attr[k] = v
                attr[k] = v
            return type.__new__(cls,name,bases,attr)
    
    class People(metaclass=Mymeta):
        school = 'oldgirl'
        addr = 'shanghai'
        def __init__(self,name,age):
            self.name = name
            self.age = age
    
    print(People.SCHOOL)
    print(People.ADDR)
    
    

    二、在元类中控制自定义的类无需init方法

    class Mymeta(type):
        def __call__(self, *args):
            obj = self.__new__(self)
            obj.name = 'nick'
            obj.age = 19
            return obj
    
    class People(metaclass=Mymeta):
       pass
    
    p = People()
    print(p.name)
    

    三、在元类中控制,把自定义的类的数据属性都放到attr字典中

    class Mymeta(type):
        def __new__(cls, name,bases,dic):
            dic2 = {'attr':{}}
            for k,v in dic.items():
                if not k.startswith('__'):
                    dic2['attr'][k] = v
            return type.__new__(cls,name,bases,dic2)
    
    class People(metaclass=Mymeta):
        school = 'oldgirl'
        addr = 'shanghai'
        def __init__(self,name,age):
            self.name = name
            self.age = age
    
    print(People.attr['school'])
    print(People.attr['addr'])
    

    四、四种方式实现单例模式

    ## 第一种
    PORT = 3306
    HOST = '127.0.0.1'
    
    class Sql:
        instance = None
        def __init__(self,port,host):
            self.port = port
            self.host = host
        @classmethod
        def get_sigo(cls):
            if not cls.instance:
                cls.cls(PORT,HOST)
            return cls.instance
    ## 第二种
    PORT = 3306
    HOST = '127.0.0.1'
    
    def get_sigo(func):
        instance = None
        def wrapper(*args,**kwargs):
            if len(args) != 0 or len(kwargs) != 0:
                #表示传了参数,生成新对象
                res = func(*args,**kwargs)
                return res
            else:
                nonlocal instance
                if not instance:
                    instance = func(PORT,HOST)
                return instance
        return wrapper
    
    @get_sigo
    class Sql:
        def __init__(self,port,host):
            self.port = port
            self.host = host
    ## 第三种
    PORT = 3306
    HOST = '127.0.0.1'
    
    class Mymeta(type):
        def __init__(self,name,bases,dic):
            # 把实例化好的对象,放到类的名称空间
            self.instance = self(PORT,HOST)
        def __call__(self,*args,**kwargs):
            if len(args) != 0 or len(kwargs) != 0:
                obj = object.__new__(self)
                obj.__init__(*args,**kwargs)
                return obj
            else:
                return self.instance
    class Sql(metaclass = Mymeta):
        def __init__(self,port,host):
            self.port=port
            self.host=host
    ## 第四种
    # sigo 文件
    # ************************************
    PORT = 3306
    HOST = '127.0.0.1'
    class Sql():
        def __init__(self,port,host):
            self.port=port
            self.host=host
    
    s1=Sql(PORT,HOST)
    # ************************************
    
    def test():
        from sigo import s1
        print(s1)
    def test2():
        from sigo import s1 as s2
        print(s2)
    
  • 相关阅读:
    MFC基于对话框的程序添加菜单
    iOS远程推送原理及实现过程
    基于SolrCloud的内容搜索和热点推送
    Android 常用抓包工具介绍之Charles
    帮你快速实现全景应用性能可视化
    Android常用抓包工具之TcpDump
    JAVA IO 序列化与设计模式
    《2015中国移动应用性能管理白皮书》欢迎来看
    利用听云Server和听云Network实测Kubernetes和Mesos在高并发下的网络性能
    魅族电商运维之路
  • 原文地址:https://www.cnblogs.com/dadazunzhe/p/11461777.html
Copyright © 2020-2023  润新知