提取页面元数据是否可以很好地利用多重继承?
我想知道我是否有几个模型,其中都包含“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于一个简单指定的问题的一般建议:
Python 中的非平凡多重继承需要高级技术来处理元类/元类型冲突。 从 ActiveState 档案中查看此食谱,看看它是否像您想要的那样喜欢:
从链接的食谱中摘录:
因此,我建议将 Python 中多重继承的使用限制为以下情况:
编辑:
这是 Guido van Rossum 在 Python 简介中的内容:
在这里,他再次出现在 PEP 253 中,其中描述了所纳入的想法进入Python,但不是实现:
这并不意味着您不应该使用多重继承;而是意味着您不应该使用多重继承。 我只是警告你,这样你就不会有一天惊讶地发现自己拍着额头大喊“天啊!
我的子类型之一的元类型不是其所有基本类型的元类型的后代! 你不讨厌这种事发生吗?”
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:
Consequently, I recommend limiting your use of multiple inheritance in Python to the following cases:
Edit:
Here's Guido van Rossum in An Introduction to Python:
Here he is again in PEP 253, which describes the ideas which were incorporated into Python, but not the implementation:
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?"