美文网首页
python魔法-metaclass

python魔法-metaclass

作者: SeanJX | 来源:发表于2019-07-18 20:04 被阅读0次

    everything is object

    python中,一切都是对象,比如一个数字、一个字符串、一个函数。对象是类(class)的是实例,类(class)也是对象,是type的实例。type对象本身又是type类的实例(鸡生蛋还是蛋生鸡?),因此我们称type为metaclass(中文元类)。


    image.png

    metaclass可以定制类的创建

    我们都是通过class OBJ(obejct):pass的方式来创建一个类,上面有提到,类(class)是type类型的实例,按照我们常见的创建类的实例(instance)的方法,那么类(class)应该就是用 class="type"(*args)的方式创建的。确实如此,python document中有明确描述:

    class type(name, bases, dict)
    With three arguments, return a new type object. This is essentially a dynamic form of the class statement. The name string is the class name and becomes the name attribute; the bases tuple itemizes the base classes and becomes the bases attribute; and the dict dictionary is the namespace containing definitions for class body and becomes the dict attribute. For example, the following two statements create identical type objects:

    该函数返回的就是一个class,三个参数分别是类名、基类列表、类的属性。比如在上面提到的OBJ类,完全等价于:OBJ = type('OBJ', (), {'a': 1})

    当然,用上面的方式创建一个类(class)看起来很傻,不过其好处在于可以动态的创建一个类。

    python将定制类开放给了开发者,type也是一个类型,那么自然可以被继承,type的子类替代了Python默认的创建类(class)的行为,什么时候需要做呢

    Some ideas that have been explored including logging, interface checking, automatic delegation, automatic property creation, proxies, frameworks, and automatic resource locking/synchronization.

    那么当我们用class OBJ(obejct):pass的形式声明一个类的时候,怎么指定OBJ的创建行为呢,那就是在类中使用metaclass

    class Metaclass(type):
        def __new__(cls, name, bases, dct):
            print 'HAHAHA'
            dct['a'] = 1
            return type.__new__(cls, name, bases, dct)
    
    print 'before Create OBJ'
    class OBJ(object):
        __metaclass__ = Metaclass
    print 'after Create OBJ'
    
    if __name__ == '__main__':
        print OBJ.a
    

    运行结果:

    before Create OBJ
    HAHAHA
    after Create OBJ
    1
    

    关于meataclass的两个细节

    • metaclass 是一个callable即可

    While in Python you can use arbitrary callables for metaclasses (like Jerub shows), the better approach is to make it an actual class itself. type is the usual metaclass in Python. type is itself a class, and it is its own type. You won't be able to recreate something like type purely in Python, but Python cheats a little. To create your own metaclass in Python you really just want to subclass type.

    • 何查找并应用metaclass

    The appropriate metaclass is determined by the following precedence rules:
      ● If dict['metaclass'] exists, it is used.
      ● Otherwise, if there is at least one base class, its metaclass is used (this looks for a class attribute first and if not found, uses its type).
      ● Otherwise, if a global variable named metaclass exists, it is used.
      ● Otherwise, the old-style, classic metaclass (types.ClassType) is used.

    对应的python源码在ceval.c::build_class,核心代码如下,很明了。

    static PyObject *
    build_class(PyObject *methods, PyObject *bases, PyObject *name)
    {
        PyObject *metaclass = NULL, *result, *base;
    
        if (PyDict_Check(methods))
            metaclass = PyDict_GetItemString(methods, "__metaclass__");
        if (metaclass != NULL)
            Py_INCREF(metaclass);
        else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
            base = PyTuple_GET_ITEM(bases, 0);
            metaclass = PyObject_GetAttrString(base, "__class__");
            if (metaclass == NULL) {
                PyErr_Clear();
                metaclass = (PyObject *)base->ob_type;
                Py_INCREF(metaclass);
            }
        }
        else {
            PyObject *g = PyEval_GetGlobals();
            if (g != NULL && PyDict_Check(g))
                metaclass = PyDict_GetItemString(g, "__metaclass__");
            if (metaclass == NULL)
                metaclass = (PyObject *) &PyClass_Type;
            Py_INCREF(metaclass);
        }
        result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
                                              NULL);
        Py_DECREF(metaclass);
        if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
            /* A type error here likely means that the user passed
               in a base that was not a class (such the random module
               instead of the random.random type).  Help them out with
               by augmenting the error message with more information.*/
    
            PyObject *ptype, *pvalue, *ptraceback;
    
            PyErr_Fetch(&ptype, &pvalue, &ptraceback);
            if (PyString_Check(pvalue)) {
                PyObject *newmsg;
                newmsg = PyString_FromFormat(
                    "Error when calling the metaclass bases\n"
                    "    %s",
                    PyString_AS_STRING(pvalue));
                if (newmsg != NULL) {
                    Py_DECREF(pvalue);
                    pvalue = newmsg;
                }
            }
            PyErr_Restore(ptype, pvalue, ptraceback);
        }
        return result;
    }
    
    ceval::build_class
    

    使用了metaclass来实现Mixin的行为,即某一个类拥有定义在其他一些类中的行为,简单来说,就是要把其他类的函数都注入到这个类。但是我们不想用继承的方法,语义上不是is a的关系,不使用继承

    import inspect
    import types
    class RunImp(object):
        def run(self):
            print 'just run'
    
    class FlyImp(object):
        def fly(self):
            print 'just fly'
    
    class MetaMixin(type):
        def __init__(cls, name, bases, dic):
            super(MetaMixin, cls).__init__(name, bases, dic)
            member_list = (RunImp, FlyImp)
    
            for imp_member in member_list:
                if not imp_member:
                    continue
                
                for method_name, fun in inspect.getmembers(imp_member, inspect.ismethod):
                    setattr(cls, method_name, fun.im_func)
    
    class Bird(object):
        __metaclass__ = MetaMixin
    
    print Bird.__dict__
    print Bird.__base__
    

    如果需要重载来自metaclass中的func

    class Bird(object):
        __metaclass__ = MetaMixin
    
    class SpecialBird(Bird):
        def run(self):
            print 'SpecialBird run'
    
    if __name__ == '__main__':
        b = SpecialBird()
        b.run()
    

    SpecialBird自身没有定义metaclass属性,那么会使用基类Bird的metaclass属性,因此虽然在SpecialBird中定义了run方法,但是会被MetaMixin给覆盖掉.

    import dis
    
    class SpecialBird(Bird):
        def run(self):
            print 'SpecialBird run'
        dis.dis(run)
    dis.dis(SpecialBird.run)
    

    验证结果:

    ... 
      3           0 LOAD_CONST               1 ('haahhah')
                  3 PRINT_ITEM          
                  4 PRINT_NEWLINE       
                  5 LOAD_CONST               0 (None)
                  8 RETURN_VALUE        
    >>> dis.dis(SpecialBird.run)
      3           0 LOAD_CONST               1 ('just run')
                  3 PRINT_ITEM          
                  4 PRINT_NEWLINE       
                  5 LOAD_CONST               0 (None)
                  8 RETURN_VALUE        
    >>> 
    

    防止方法覆盖,可以在setter前检查有效性

    重复使用metaclass

    >>> class DummyMeta(type):
    ...     pass
    ... 
    >>> 
    >>> class SB(Bird):
    ...     __metaclass__ = DummyMeta
    ... 
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: Error when calling the metaclass bases
        metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
    

    类的metaclass必须继承自基类的metaclass

    metaclass __new__/ __init__

    用过metaclass的pythoner可能会有疑问,因为网上的很多case都是在metaclass中重载type的new方法,而不是init。实时上,对于我们使用了MetaMixin,也可以通过重载new方法实现
    重载new方法

    new() is intended mainly to allow subclasses of immutable types (like int, str, or tuple) to customize instance creation. It is also commonly overridden in custom metaclasses in order to customize class creation.

    即用于继承不可变对象,或者使用在metaclass中!

    class Meta(type):
        def __new__(cls, name, bases, dic):
            print 'here class is %s' % cls
            print 'class %s will be create with bases class %s and attrs %s' % (name, bases, dic.keys())
            dic['what'] = name
            return type.__new__(cls, name, bases, dic)
    
        def __init__(cls, name, bases, dic):
            print 'here class is %s' % cls
            print 'class %s will be inited with bases class %s and attrs %s' % (name, bases, dic.keys())
            print cls.what
            super(Meta, cls).__init__(name, bases, dic)
    
    class OBJ(object):
        __metaclass__ = Meta
        attr = 1
    
    print('-----------------------------------------------')
    class SubObj(OBJ):
        pass
    
    here class is <class '__main__.Meta'>
    
    class OBJ will be create with bases class (<type 'object'>,) and attrs ['__module__', '__metaclass__', 'attr']
    here class is <class '__main__.OBJ'>
    class OBJ will be inited with bases class (<type 'object'>,) and attrs ['__module__', '__metaclass__', 'attr', 'what']
    OBJ
    -----------------------------------------------
    here class is <class '__main__.Meta'>
    class SubObj will be create with bases class (<class '__main__.OBJ'>,) and attrs ['__module__']
    here class is <class '__main__.SubObj'>
    class SubObj will be inited with bases class (<class '__main__.OBJ'>,) and attrs ['__module__', 'what']
    SubObj
    
    • 首先要注意虽然在new init方法的第一个参数都是cls,但是完全是两回事!

    • 然后在调用new之后,产生的类对象(cls如OBJ)就已经有了动态添加的what 属性

    • 在调用new的时候,dic只来自类的scope内所定义的属性,所以在创建SubObj的时候,dic里面是没有属性的,attr在基类OBJ的dict里面,也能看出在new中修改后的dic被传入到init方法当中。

    参考

    what-is-a-metaclass-in-python

    深刻理解Python中的元类(metaclass)

    customizing-class-creation

    special-method-names

    https://www.cnblogs.com/xybaby/p/7927407.html#_label_9

    相关文章

      网友评论

          本文标题:python魔法-metaclass

          本文链接:https://www.haomeiwen.com/subject/ydkrlctx.html