• metaclass


    基本常识

    1.对象是类创建,创建对象时候类的__init__方法自动执行,对象()执行类的 __call__ 方法
    2.类是type创建,创建类时候type的__init__方法自动执行,类() 执行type的 __call__方法(类的__new__方法,类的__init__方法)
    
    # 第0步: 执行type的 __init__ 方法【类是type的对象】
    class Foo:
        def __init__(self):
            pass
    
        def __call__(self, *args, **kwargs):
            pass
    
    # 第1步: 执行type的 __call__ 方法
    #        1.1  调用 Foo类(是type的对象)的 __new__方法,用于创建对象。
    #        1.2  调用 Foo类(是type的对象)的 __init__方法,用于对对象初始化。
    obj = Foo()
    # 第2步:执行Foodef __call__ 方法
    obj()

    示例一

    class MyType(type):
        def __init__(self, *args, **kwargs):
            print('MyType创建类',self)
            super(MyType, self).__init__(*args, **kwargs)
    
        def __call__(self, *args, **kwargs):
            obj = super(MyType, self).__call__(*args, **kwargs)
            print('类创建对象', self, obj)
            return obj
    
    
    class Foo(object,metaclass=MyType):
        user = 'ctz'
        age = 18
    
    obj = Foo()
    View Code

    结果:

    MyType创建类 <class '__main__.Foo'>
    类创建对象 <class '__main__.Foo'> <__main__.Foo object at 0x0000024858243470>

    示例二:

    class MyType(type):
        def __init__(self, *args, **kwargs):
            print('MyType',self,'----')
            super(MyType, self).__init__(*args, **kwargs)
    
        def __call__(cls, *args, **kwargs):
            v = dir(cls)
            obj = super(MyType, cls).__call__(*args, **kwargs)
            print('MyType',cls,obj,'****')
            return obj
    
    
    class Foo(MyType('MyType', (object,), {})):
        user = 'ctz'
        age = 18
    
    
    obj = Foo()
    View Code

    结果

    MyType <class '__main__.MyType'> ----
    MyType <class '__main__.Foo'> ----
    MyType <class '__main__.Foo'> <__main__.Foo object at 0x0000025A92BB36D8> ****

    示例三:(查看wtforms源码可知,实例化Form就是通过这种方法做的,此外这种方式可以实现单例模式)

    class MyType(type):
        def __init__(self, *args, **kwargs):
            print(self,'------')
            super(MyType, self).__init__(*args, **kwargs)
    
        def __call__(cls, *args, **kwargs):
            v = dir(cls)
            obj = super(MyType, cls).__call__(*args, **kwargs)
            print(cls,obj,'****')
            return obj
    
    
    def with_metaclass(arg,base):
        return MyType('MyType', (base,), {})
    
    
    class Foo(with_metaclass(MyType,object)):
        user = 'ctz'
        age = 18
    
    
    obj = Foo()
    View Code

    结果:

    <class '__main__.MyType'> ------
    <class '__main__.Foo'> ------
    <class '__main__.Foo'> <__main__.Foo object at 0x000001FBD9E63710> ****

     后面由于在公司做分享,没什么分享的,就分享了元类 自己整理了一篇在简书上链接如下:https://www.jianshu.com/p/f3de04763481

  • 相关阅读:
    nginx 安全请求头
    使用citus 列式存储压缩数据
    nginx ngx_http_realip 的功能以及使用
    act 的密钥&&环境变量管理
    oracle怎么查询重复的数据
    如何在Oracle中复制表结构和表数据
    2022成都.NET开发者Connect线下活动
    闭包具有逻辑内聚的功能
    编程范式是人类思维方式的投影代表了程序设计者认为程序应该如何被构建和执行的看法
    工程师是高级生产者
  • 原文地址:https://www.cnblogs.com/ctztake/p/8259785.html
Copyright © 2020-2023  润新知