程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

什么是Python中的元类?

发布于2019-08-19 21:34     阅读(2011)     评论(0)     点赞(9)     收藏(1)


什么是元类,我们用它们做什么?


解决方案


元类是类的类。类定义了类的实例(即对象)的行为,而元类定义了类的行为方式。类是元类的实例。

虽然在Python中你可以为元类使用任意的callables(比如Jerub节目),但更好的方法是让它成为一个真正的类本身。type是Python中常用的元类。type它本身就是一个类,它是它自己的类型。你将无法type在Python中重新创建纯粹的东西,但是Python会有所作为要在Python中创建自己的元类,你真的只想要子类type

元类最常用作类工厂。当您通过调用类创建对象时,Python通过调用元类创建一个新类(当它执行'class'语句时)。结合法线__init____new__方法,元类因此允许您在创建类时执行“额外的事情”,例如使用某些注册表注册新类或者完全替换其他类。

class被执行的语句,Python的首先执行的主体class声明为代码的正常块。生成的命名空间(dict)保存了将要进行的类的属性。元类是通过查看待定类的基类(继承的元类),在待定类的__metaclass__属性(如果有)或__metaclass__全局变量来确定的。然后使用类的名称,基数和属性调用元类来实例化它。

但是,元类实际上定义了类的类型,而不仅仅是它的工厂,所以你可以用它们做更多的事情。例如,您可以在元类上定义常规方法。这些元类方法就像类方法一样,它们可以在没有实例的类上调用,但它们也不像类方法,因为它们不能在类的实例上调用。type.__subclasses__()type元类的方法示例您还可以定义常规的“魔术”方法,例如__add____iter__以及__getattr__实现或更改类的行为方式。

这是比特和碎片的汇总示例:

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__


所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/48889/7b7a77f832e6820a3ac9/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

9 0
收藏该文
已收藏

评论内容:(最多支持255个字符)