python魔法-metaclass

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,651评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,468评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,931评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,218评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,234评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,198评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,084评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,926评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,341评论 1 311
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,563评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,731评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,430评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,036评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,676评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,829评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,743评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,629评论 2 354