Didn"t forge 2008-09-19 06:10 采纳率: 25%
浏览 417
已采纳

Python 中的元类是什么?

What are metaclasses and what do we use them for?

转载于:https://stackoverflow.com/questions/100003/what-are-metaclasses-in-python

  • 写回答

15条回答 默认 最新

  • hurriedly% 2008-09-19 07:01
    关注

    A metaclass is the class of a class. Like a class defines how an instance of the class behaves, a metaclass defines how a class behaves. A class is an instance of a metaclass.

    While in Python you can use arbitrary callables for metaclasses (like Jerub shows), the more useful approach is actually to make it an actual class itself. type is the usual metaclass in Python. In case you're wondering, yes, 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.

    A metaclass is most commonly used as a class-factory. Like you create an instance of the class by calling the class, Python creates a new class (when it executes the 'class' statement) by calling the metaclass. Combined with the normal __init__ and __new__ methods, metaclasses therefore allow you to do 'extra things' when creating a class, like registering the new class with some registry, or even replace the class with something else entirely.

    When the class statement is executed, Python first executes the body of the class statement as a normal block of code. The resulting namespace (a dict) holds the attributes of the class-to-be. The metaclass is determined by looking at the baseclasses of the class-to-be (metaclasses are inherited), at the __metaclass__ attribute of the class-to-be (if any) or the __metaclass__ global variable. The metaclass is then called with the name, bases and attributes of the class to instantiate it.

    However, metaclasses actually define the type of a class, not just a factory for it, so you can do much more with them. You can, for instance, define normal methods on the metaclass. These metaclass-methods are like classmethods, in that they can be called on the class without an instance, but they are also not like classmethods in that they cannot be called on an instance of the class. type.__subclasses__() is an example of a method on the type metaclass. You can also define the normal 'magic' methods, like __add__, __iter__ and __getattr__, to implement or change how the class behaves.

    Here's an aggregated example of the bits and pieces:

    def make_hook(f):
        """Decorator to turn 'foo' method into '__foo__'"""
        f.is_hook = 1
        return f
    
    class MyType(type):
        def __new__(mcls, name, bases, attrs):
    
            if name.startswith('None'):
                return None
    
            # Go over attributes and see if they should be renamed.
            newattrs = {}
            for attrname, attrvalue in attrs.iteritems():
                if getattr(attrvalue, 'is_hook', 0):
                    newattrs['__%s__' % attrname] = attrvalue
                else:
                    newattrs[attrname] = attrvalue
    
            return super(MyType, mcls).__new__(mcls, name, bases, newattrs)
    
        def __init__(self, name, bases, attrs):
            super(MyType, self).__init__(name, bases, attrs)
    
            # classregistry.register(self, self.interfaces)
            print "Would register class %s now." % self
    
        def __add__(self, other):
            class AutoClass(self, other):
                pass
            return AutoClass
            # Alternatively, to autogenerate the classname as well as the class:
            # return type(self.__name__ + other.__name__, (self, other), {})
    
        def unregister(self):
            # classregistry.unregister(self)
            print "Would unregister class %s now." % self
    
    class MyObject:
        __metaclass__ = MyType
    
    
    class NoneSample(MyObject):
        pass
    
    # Will print "NoneType None"
    print type(NoneSample), repr(NoneSample)
    
    class Example(MyObject):
        def __init__(self, value):
            self.value = value
        @make_hook
        def add(self, other):
            return self.__class__(self.value + other.value)
    
    # Will unregister the class
    Example.unregister()
    
    inst = Example(10)
    # Will fail with an AttributeError
    #inst.unregister()
    
    print inst + inst
    class Sibling(MyObject):
        pass
    
    ExampleSibling = Example + Sibling
    # ExampleSibling is now a subclass of both Example and Sibling (with no
    # content of its own) although it will believe it's called 'AutoClass'
    print ExampleSibling
    print ExampleSibling.__mro__
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(14条)

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题