- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
你好 Stackoverflow 社区
我一直在努力了解 Django(和 Wagtail 的 Stream-field)在幕后是如何工作的。通过这样做,我了解了元类并相信掌握了原理。也就是说,SIX 究竟如何执行 with_metaclass 函数对我来说仍然有点模糊。这是代码后跟一个特定的问题:
模型.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 > block > stream_block.py
class StreamBlock(six.with_metaclass(DeclarativeSubBlocksMetaclass, BaseStreamBlock)):
pass
六.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] 中定义的类 元类。此类的实例包含 DeclarativeSubBlockMetaclass 作为类型,'temporary_class' 作为没有基或属性的名称。
在 [1] 中,我们定义了 元类 类,它似乎在执行实际的元类工作。在这里,我们开发了一个类生成器,它根据基类和名称生成 DeclarativeSubBlockMetaclass 类型的类(作为元传入)。
但是,由于对 [1] 的唯一调用来自 [2],我们所做的似乎只是实例化 DeclarativeSubBlockMetaclass 类型的“temporary_class”,没有任何基础或属性。
我们如何用描述 (1) 中描述的实际元类替换这个临时虚拟元类?
为此,我尝试查阅 six 的文档,但找不到任何可以解决我困惑的内容。
如有任何建议,我们将不胜感激。
非常感谢Z
仅供引用:
我包含了上面 six.with_metaclass 调用中使用的两个类的代码:
DeclarativeSubBlocksMetaclass
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
基本流 block
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()
最佳答案
好的 - 我想我明白了。问题的症结在于
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
关于python - 哇 six.with_metaclass() 有用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42825295/
当我开始学习一门新语言时,我总是觉得我没有以实用、标准的方式进行学习。所以这里有一个关于 jQuery 的问题以及我所做的是否可以接受。 我有 3 张图片。 然后我让 jQuery 检测 $('
基本上,我想知道线程是否有用或必要,或者可能更具体地说,您将使用它的用途和情况。我对线程了解不多,也从未使用过它(我主要使用 C#),并且想知道如果使用它们是否会提高性能或稳定性。如果有人愿意解释一下
这个问题在这里已经有了答案: What is The Rule of Three? (8 个答案) 关闭 7 年前。 嘿嘿。我有一个让我很难过的问题。我自定义了一个普通的拷贝构造函数但它只在我初始化
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: Uses for multiple levels of pointer dereferences? 我在 C
我不确定异常在每种语言中的工作方式是否相同,但我使用的是 PHP,我想知道我什么时候做这样的事情: if (!$this->connection[0]->query($this->query)) t
Scala native 是最近发布的,但是他们(现在)使用的垃圾收集器非常rudimentary,因此不适合严肃使用。 所以我想知道:为什么不只将Scala转换为Go (即Scala.js)?这将是
最近,我一直在研究docker及其对SaaS公司的有用性。我花了一些时间学习如何容器化应用程序,并简要了解了什么是docker和容器。我在理解这项技术的实用性时遇到了一些问题。我看过dockercon
我必须根据出现在它们之前的字符串是否是某个关键字“load”从输入文件中读取整数。没有关键数字告诉我们要输入多少个数字。这些数字必须保存到一个数组中。为了避免为扫描的每个附加数字创建和更新新数组,我想
Deferred 对象具有回调池 doneCallbacks、failCallbacks 和 progressCallbacks。 doneCallbacks 和 failCallbacks(以及方法
这个问题在这里已经有了答案: Is there a case where including the same header twice is actually helpful? (6 个答案) 关
我在C++ Programming Language的书上看到了下面的例子 class Ptr { X* operator->( ); }; voide f(Ptr p) { p-
你能不能: template const T &operator[] (unsigned int x) 我的想法是如果你有一个 map如果有一个包装器类可以让您这样做,那就太好了: obj["Int
根据doc这个tutorial , cmp() returns -1 if x y 教程里也说了 cmp() returns the sign of the difference of two nu
我经常读到 It seem that identity monad is useless. It's not... but that's another topic. 那么谁能告诉我它有什么用? 最佳
我已经知道实现和接口(interface)的基础知识。我不明白什么时候使用接口(interface)。有接口(interface)的要求是什么? 例子: /// Interface demo Inte
在一些 R 函数的主体中,例如 lm,我看到对 match.call 函数的调用。正如其帮助页面所述,当在函数内部使用 match.call 时,会返回指定参数名称的调用;这对于将大量参数传递给另一个
在监督学习中,我有典型的训练/测试分割来学习算法,例如回归或分类。关于无监督学习,我的问题是:训练/测试分割是否必要且有用?如果是,为什么? 最佳答案 这取决于问题、数据集的形式以及用于解决特定问题的
我最近接触到 Javascript 模板并变得非常感兴趣。 我正在使用 MVC 模式构建一个大型 PHP 应用程序。模板由相当棒的 Twig 处理. 我最近遇到了一个 javascript imple
我最近在一个我要重构并拥有的项目中遇到了以下代码行: SomeClass someClass = new SomeClass(); 我这辈子都想不通为什么有人会以这种方式使用泛型。我想出的唯一原因是
亲爱的,我正在阅读这篇关于通过 asp.net 4 中的代码动态添加元标记的帖子 - 但我想问一下对 SEO 有什么好处,静态添加它或者在代码后面添加它没有问题 http://weblogs.asp.
我是一名优秀的程序员,十分优秀!