提取页面元数据是否可以很好地利用多重继承?

发布于 2024-07-25 01:14:01 字数 136 浏览 3 评论 0原文

我想知道我是否有几个模型,其中都包含“meta_keywords”或“slug”等字段,这些字段与模型实例将在其上显示的网页有关,是否建议将这些页面元数据元素分解出来进入他们自己的类,比如 PageMeta,并且我的其他模型是否通过多重继承来子类化这些模型?

I was wondering if I have a couple of models which both include fields like "meta_keywords" or "slug" which have to do with the web page the model instance will be displayed on, whether it would be advisable to break those page metadata elements out into their own class, say PageMeta, and have my other models subclass those via multiple inheritance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ˉ厌 2024-08-01 01:14:01

对于一个简单指定的问题的一般建议:

Python 中的非平凡多重继承需要高级技术来处理元类/元类型冲突。 从 ActiveState 档案中查看此食谱,看看它是否像您想要的那样喜欢:

从链接的食谱中摘录:

最简单的情况是元类型
发生冲突的情况如下。
考虑一个带有元类 M_A 的 A 类
和一个具有独立的B类
元类M_B; 假设我们推导 C
来自 A 和 B。问题是:什么是
C 的元类? 是 M_A 还是 M_B

正确答案(见书上
“让元类发挥作用”
深思熟虑的讨论)是 M_C,其中
M_C 是一个元类,继承自
M_AM_B

然而,Python 并没有那么神奇,而且
它不会自动创建M_C
相反,它会引发 TypeError,
警告程序员可能发生的情况
混乱。

因此,我建议将 Python 中多重继承的使用限制为以下情况:

  1. 必须这样做,因为您的问题域要求您组合两个单独维护的单继承库。
  2. 您已经非常熟练地使用元类型和元类,可以编写 recipe 204197 或其等效内容:轻松自信地编写打印语句。

编辑:

这是 Guido van Rossum 在 Python 简介中的内容:

很明显,滥用
多重继承是一种维护
噩梦,鉴于对
Python 中要避免的约定
意外的名称冲突。

在这里,他再次出现在 PEP 253 中,其中描述了所纳入的想法进入Python,但不是实现:

元类型决定各种策略
对于类型,例如什么
当调用类型时发生,动态类型如何(无论是
type 的 dict 创建后可以修改),什么
方法解析顺序是,如何查看实例属性
向上,依此类推。

我认为从左到右深度优先并不是最好的
当您想充分利用多个资源时的解决方案
继承。

我认为,通过多重继承,
子类型必须是所有基本类型的元类型的后代。

这并不意味着您不应该使用多重继承;而是意味着您不应该使用多重继承。 我只是警告你,这样你就不会有一天惊讶地发现自己拍着额头大喊“天啊!
我的子类型之一的元类型不是其所有基本类型的元类型的后代! 你不讨厌这种事发生吗?”

General advice for a lightly-specified question:

Nontrivial multiple inheritance in Python requires Advanced Techniques to deal with the metaclass/metatype conflict. Look over this recipe from the ActiveState archives and see if it looks like the kind of stuff you like:

Extract from linked recipe:

The simplest case where a metatype
conflict happens is the following.
Consider a class A with metaclass M_A
and a class B with an independent
metaclass M_B; suppose we derive C
from A and B. The question is: what is
the metaclass of C ? Is it M_A or M_B
?

The correct answer (see the book
"Putting metaclasses to work" for a
thoughtful discussion) is M_C, where
M_C is a metaclass that inherits from
M_A and M_B.

However, Python is not that magic, and
it does not automatically create M_C.
Instead, it raises a TypeError,
warning the programmer of the possible
confusion.

Consequently, I recommend limiting your use of multiple inheritance in Python to the following cases:

  1. You must, because your problem domain requires you to combine two separately-maintained single-inheritance libraries.
  2. You have achieved such fluency with metatype and metaclass that you can write recipe 204197 or its equivalent as easily and confidently as you can write a print statement.

Edit:

Here's Guido van Rossum in An Introduction to Python:

It is clear that indiscriminate use of
multiple inheritance is a maintenance
nightmare, given the reliance in
Python on conventions to avoid
accidental name conflicts.

Here he is again in PEP 253, which describes the ideas which were incorporated into Python, but not the implementation:

Metatypes determine various policies
for types, such as what
happens when a type is called, how dynamic types are (whether a
type's dict can be modified after it is created), what the
method resolution order is, how instance attributes are looked
up, and so on.

I'll argue that left-to-right depth-first is not the best
solution when you want to get the most use from multiple
inheritance.

I'll argue that with multiple inheritance, the metatype of the
subtype must be a descendant of the metatypes of all base types.

This does not mean you shouldn't use multiple inheritance; I'm just warning you so you won't be suprised one day to find yourself slapping your forehead and exclaiming "D'oh!
The metatype of one of my subtypes isn't a descendant of the metatypes of all its base types! Don't you hate when that happens?"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文