Python元类有什么用? [英] What are Python metaclasses useful for?

查看:159
本文介绍了Python元类有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不能以其他任何方式使用的元类可以做什么?

What can be done with metaclasses that can't be in any other way?

Alex Martelli告诉我们,如果没有元类,有些任务是无法完成的 Python元类与类装饰器 我想知道是什么?

Alex Martelli told that there are tasks that can't be achieved without metaclasses here Python metaclasses vs class decorators I'd like to know which are?

推荐答案

如果您想让配备有特殊自定义行为"的类对象(与类对象的 instance 相反),元类是必不可少的,因为对象的行为取决于对象的 type 上的特殊方法,而类对象的类型恰好是元类的同义词.

Metaclasses are indispensable if you want to have class objects (as opposed to instances of class objects) equipped with "special customized behavior", since an object's behavior depends on special methods on the type of the object, and a class object's type is, exactly a synonym for, the metaclass.

例如,如果您想要一个类对象X,以使"print X"发出"Time is now 8:46 am"(现在是8:46 am,或更普遍的是当前时间),则必须表示(AKA X的元类)具有一个特殊的自定义__str__方法-并且,如果要给诸如X + Y这样的表达式(其中X和Y都是类对象)赋予含义,则具有类似的自定义方法(具有各种适用的特殊方法) X[23](其中X再次是类对象),依此类推.

For example, if you want a class object X such that "print X" emits "Time is now 8:46am" (at 8:46 am, or, more generally, the current time) this must mean that type(x) (AKA X's metaclass) has a special custom __str__ method -- and similarly (with the various applicable special-methods) if you want to give meaning to expressions such as X + Y where X and Y are both class objects, or X[23] (where X, again, is a class object), and so forth.

现在,大多数其他自定义任务(在Python 2.6或更高版本中)更容易使用类装饰器实现,该装饰器可以在class语句结束后立即更改类对象.在另一些情况下,这是不可行的,因为要使更改生效(例如,设置或更改__slots__),必须尽早进行更改.

Most other customization tasks are now (in Python 2.6 or better) easier to implement with a class decorator, which can alter a class object right after the end of the class statement. There are a few more cases where this is not feasible because the alterations must be made very early on if they are to have any effect (e.g., setting or altering __slots__).

在Python 3中,元类获得了更多的用处:元类现在可以选择指定在执行class语句的正文期间要填充的映射对象(默认情况下,这是普通的dict).这样可以保留和使用类主体中名称绑定的 order (而普通的dict会丢失顺序),这在类必须具有特定字段中的字段"时有时会很好顺序(例如,将1:1映射到C struct,CSV文件或DB表中的一行,等等)-在Python 2 *中,必须冗余指定(通常使用额外的class属性一个序列,因此确实保留了顺序),并且Python 3元类的此功能允许删除冗余.

In Python 3, metaclasses gain one extra little bit of usefulness: a metaclass can now optionally specify the mapping object to be populated during the execution of the class statement's body (by default, it's a normal dict). This allows the order of name bindings in the class body to be preserved and used (while the normal dict loses order), which is sometimes nice when the class must have "fields" in a certain specific order (e.g. to map 1:1 onto a C struct, a row in a CSV file or DB table, and the like) -- in Python 2.* this had to be redundantly specified (typically with an extra class attribute that's a sequence and thus does preserve order), and this feature of Python 3 metaclasses allows the redundancy to be removed.

这篇关于Python元类有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆