哇, Six.with_metaclass() 有效吗?

Posted

技术标签:

【中文标题】哇, Six.with_metaclass() 有效吗?【英文标题】:Wow does six.with_metaclass() work? 【发布时间】:2017-08-07 02:36:52 【问题描述】:

你好 *** 社区

我一直试图了解 Django(和 Wagtail 的 Stream-field)如何在幕后工作。这样做我了解了元类,并相信掌握了这一原则。也就是说,SIX 如何执行 with_metaclass 函数对我来说仍然有点模糊。下面是代码,后面是一个特定的问题:

models.py

class BlogPage(Page):
    blogElement = StreamField([
        ('heading', blocks.CharBlock(classname="full title")),
        ('paragraph', blocks.TextBlock()),
        ('picture', ImageChooserBlock()),
    ], default=[])

wagtailcore > fields.py

class StreamField(models.Field):
    def __init__(self, block_types, **kwargs):
        if isinstance(block_types, Block):
            self.stream_block = block_types
        elif isinstance(block_types, type):
            self.stream_block = block_types()
        else:
            self.stream_block = StreamBlock(block_types)
        super(StreamField, self).__init__(**kwargs)

wagtailcore > 块 > stream_block.py

class StreamBlock(six.with_metaclass(DeclarativeSubBlocksMetaclass, BaseStreamBlock)):
    pass

six.py

def with_metaclass(meta, *bases):
    """Create a base class with a metaclass."""
    # This requires a bit of explanation: the basic idea is to make a dummy
    # metaclass for one level of class instantiation that replaces itself with
    # the actual metaclass.
    class metaclass(meta):

        def __new__(cls, name, this_bases, d):
            return meta(name, bases, d)
    return type.__new__(metaclass, 'temporary_class', (), )

问题

(1) 描述建议我们生成一个临时的虚拟元类,用实际的元类替换自己。 (2) 这是如何工作的? (3) 我们如何通过 with_metaclass 函数对元类生成进行排序? (4)BaseStreamBlock又是从哪里来的呢?

让我困惑的部分是我们定义了

[1] class metaclass(meta):

但只能通过以下方式调用它:

[2] return type.__new__(metaclass, 'temporary_class', (), )

在 [2] 中,我们实例化了在 [1] 中定义的类 metaclass。该类的实例包含 DeclarativeSubBlockMetaclass 作为类型,并以 'temporary_class' 作为名称,没有基础或属性。

在 [1] 中,我们定义了 metaclass 类,它似乎正在执行实际的元类工作。在这里,我们开发了一个类生成器,它根据基数和名称生成 DeclarativeSubBlockMetaclass 类型的类(作为元传递)。

但是,由于对 [1] 的唯一调用来自 [2],我们似乎正在做的只是实例化类型为 DeclarativeSubBlockMetaclass 的“temporary_class”,而无需任何基础或属性。

我们如何用描述 (1) 中描述的实际元类替换这个临时的虚拟元类?

我试图为此查阅六人的文档,但找不到任何可以解决我困惑的东西。

任何建议将不胜感激。

非常感谢 Z

仅供参考:

我在上面的 Six.with_metaclass 调用中包含了两个类的代码:

声明性子块元类

class DeclarativeSubBlocksMetaclass(BaseBlock):
    """
    Metaclass that collects sub-blocks declared on the base classes.
    (cheerfully stolen from      https://github.com/django/django/blob/master/django/forms/forms.py)
    """
    def __new__(mcs, name, bases, attrs):
        # Collect sub-blocks declared on the current class.
        # These are available on the class as `declared_blocks`
        current_blocks = []
        for key, value in list(attrs.items()):
            if isinstance(value, Block):
                current_blocks.append((key, value))
                value.set_name(key)
                attrs.pop(key)
        current_blocks.sort(key=lambda x: x[1].creation_counter)
        attrs['declared_blocks'] = collections.OrderedDict(current_blocks)

        new_class = (super(DeclarativeSubBlocksMetaclass, mcs).__new__(mcs, name, bases, attrs))

        # Walk through the MRO, collecting all inherited sub-blocks, to make
        # the combined `base_blocks`.
        base_blocks = collections.OrderedDict()
        for base in reversed(new_class.__mro__):
            # Collect sub-blocks from base class.
            if hasattr(base, 'declared_blocks'):
                base_blocks.update(base.declared_blocks)

            # Field shadowing.
            for attr, value in base.__dict__.items():
                if value is None and attr in base_blocks:
                    base_blocks.pop(attr)
        new_class.base_blocks = base_blocks

        return new_class

BaseStreamBlock

class BaseStreamBlock(Block):

    def __init__(self, local_blocks=None, **kwargs):
        self._constructor_kwargs = kwargs

        super(BaseStreamBlock, self).__init__(**kwargs)

        # create a local (shallow) copy of base_blocks so that it can be supplemented by local_blocks
        self.child_blocks = self.base_blocks.copy()
        if local_blocks:
            for name, block in local_blocks:
                block.set_name(name)
                self.child_blocks[name] = block

        self.dependencies = self.child_blocks.values()

【问题讨论】:

【参考方案1】:

好的 - 我想我明白了。问题的症结在于

return meta(name, bases, d)

with_metaclass 函数:

def with_metaclass(meta, *bases):
    """Create a base class with a metaclass."""
    # This requires a bit of explanation: the basic idea is to make a dummy
    # metaclass for one level of class instantiation that replaces itself with
    # the actual metaclass.
    class metaclass(meta):

        def __new__(cls, name, this_bases, d):
            return meta(name, bases, d)
    return type.__new__(metaclass, 'temporary_class', (), )

这是我认为它在 sudo 代码中的工作方式:

(1) with_metaclass takes <<DeclarativeSubBlocksMetaclass>> as meta; and <<BaseStreamBlock>> as bases
(2) class metaclass(meta) --> the class metaclass is then created extending <<DeclarativeSubBlockMetaclass>> as the class type
(3) def __new__(cls, name, this_bases, d): Only rarely will you have to worry about __new__. Usually, you'll just define __init__ and let the default __new__ pass the constructor arguments to it. __new__ takes care of creating the object and assigning memory space to it. This __new__ method is a class method that gets called when you create an instance of the class and it gets called before __init__.  Its main job is to allocate the memory that the object that you are creating uses. It can also be used to set up any aspect of the instance of the class that is immutable Because classes are kind of immutable (they cannot be changed), overloading __new_ is the best place to overload how they are created.
(4) return meta(name, bases, d) --> the class definition ends with returning a <<DeclarativeSubBlockMetaclass>> with the arguments (name, base = BaseStreamBlock, d)

NOTE: We only define the class in 1 - 3; we are not instantiating it this comes below

(5) return type.__new__(metaclass, 'temporary_class', (), ) --> Here we are using the classic metaclass syntax. This syntax usually looks like this: return type.__new__(cls, name, bases, attrs). We are using this syntax to instantiate the metaclass we defined in (3) and (4). One might think that it is confusing that temporary_class', (),  are passed on as the 'name', 'bases', and 'attrs' arguments. BUT...
(6) ... when the instantiation arrives at return meta(name,bases,d) we notice that meta doesn't take 'this_bases' as an argument but 'bases'. It derives this value from the arguments which were passed to (1) with_metaclasses. As such bases in this instance == <<BaseStreamBlock>>
(7) Therefore, when we instantiate type.__new__(metaclass, 'temporary_class', (), ) we essentially execute <<DeclarativeSubBlocksMetaClass>>('temporary_class', <<BaseStreamBlock>>, )

(7)中解释的步骤就是解释所谈到的。本质上,SIX 所做的是通过规定的步骤来创建一个名为temporary_class 的虚拟元类。由于 DeclarativeSubBlocksMetaClass 也是一个元类,因此它使用 BaseStreamBlock 基生成一个新类。

我希望这是有道理的。

Z

【讨论】:

我花了更多的时间来尝试理解您的答案,而不是六的代码。是的 - 这很棘手,但你明白了它的核心:问题是创建的临时元类从未实际实例化:它的 __new__ 方法改为实例化原始元类。

以上是关于哇, Six.with_metaclass() 有效吗?的主要内容,如果未能解决你的问题,请参考以下文章

apscheduler 绿色版

你如何捕捉到这个异常?

移动设备上的网页中断(哇,快!)

哇~好吃的薯片,,什么?山药碎片?

哇哈哈哈,Tang脚本语言初步成果

哇!这就是HDFS!