zoukankan      html  css  js  c++  java
  • 自定义创建元类

    #coding=utf-8
    
    class UpperAttrMetaClass(type):
        # __new__ 是在__init__之前被调用的特殊方法
        # __new__是用来创建对象并返回之的方法
        # 而__init__只是用来将传入的参数初始化给对象
        # __new__能够控制对象的创建
        # 这里,创建的对象是类,自定义这个类,我们这里改写__new__
        # 如果你希望的话,你也可以在__init__中做些事情
        # 可改写__call__特殊方法
        def __new__(cls, future_class_name, future_class_parents, future_class_attr):
            # cls、类名、父类、需要修改的字典
            #遍历属性字典,把不是__开头的属性名字变为大写
            newAttr = {}
            for key,value in future_class_attr.items():
                if not key.startswith("__"):
                    newAttr[key.upper()] = value
                    #使字典的键值大写
    
            # 方法1:通过'type'来做类对象的创建
            # return type(future_class_name, future_class_parents, newAttr)
            # type  类名、父类名、字典(刚刚进行修改的字典)
            # 方法2:复用type.__new__方法
            # 这就是基本的OOP编程,没什么魔法
            # return type.__new__(cls, future_class_name, future_class_parents, newAttr)
            # 类名、父类名、字典(刚刚进行修改的字典)
            # 方法3:使用super方法
            return super(UpperAttrMetaClass,cls).__new__(cls, future_class_name, future_class_parents, newAttr)
    
    
    
    # python3的用法
    class Foo(object, metaclass = UpperAttrMetaClass):
        # metaclass运行类的时候,根据metaclass的属性。修改类中的属性
        bar = 'bip'
    # hasattr 查看类中是否具有该属性
    print(hasattr(Foo, 'bar'))
    # 输出: False
    print(hasattr(Foo, 'BAR'))
    # 输出:True
    
    f = Foo()
    # 进行构造,产生 f 对象
    print(f.BAR)
    # 输出:'bip',metaclass修改了Foo类

    class UpperAttrMetaClass(type):
        # __new__ 是在__init__之前被调用的特殊方法
        # __new__是用来创建对象并返回之的方法
        # 而__init__只是用来将传入的参数初始化给对象
        # 你很少用到__new__,除非你希望能够控制对象的创建
        # 这里,创建的对象是类,我们希望能够自定义它,所以我们这里改写__new__
        # 如果你希望的话,你也可以在__init__中做些事情
        # 还有一些高级的用法会涉及到改写__call__特殊方法,但是我们这里不用
        def __new__(cls, future_class_name, future_class_parents, future_class_attr):
            #遍历属性字典,把不是__开头的属性名字变为大写
            newAttr = {}
            for name,value in future_class_attr.items():
                if not name.startswith("__"):
                    newAttr[name.upper()] = value
            # 方法1:通过'type'来做类对象的创建
            return type(future_class_name, future_class_parents, newAttr)
            # 方法2:复用type.__new__方法
            # return type.__new__(cls, future_class_name, future_class_parents, newAttr)
            #return type.__new__(cls,future_class_name,future_class_parents,newAttr)
            # 方法3:使用super方法
            return super(UpperAttrMetaClass, cls).__new__(cls, future_class_name, future_class_parents, newAttr)
    # python3的用法
    class Foo(object, metaclass = UpperAttrMetaClass):
       bar = 'bip'
    print(hasattr(Foo, 'bar'))
    
    print(hasattr(Foo, 'BAR'))
    f = Foo()
    print(f.BAR)

    2020-05-08

  • 相关阅读:
    Eclipse的自动排版设置(format)
    Java中" "表示几个空格
    cookie和session详解
    IO流操作详解
    springmvc常用注解标签详解
    mavenWeb工程建立步骤
    数据导出为excel表格
    Springmvc jar包介绍
    【初级算法】5.只出现一次的数字
    【初级算法】4.存在重复
  • 原文地址:https://www.cnblogs.com/hany-postq473111315/p/12846996.html
Copyright © 2011-2022 走看看