Would extracting page metadata be a good use of multiple inheritance?


Would extracting page metadata be a good use of multiple inheritance?



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?




Configuring Django to use remote mysql server?

1:



Apache/Django freezing after a few requests
General advice for a lightly-specified question:.
HTTPResponse - Return organised data (table?)
Nontrivial multiple inheritance in Python requires Advanced Techniques to deal with the metaclass/metatype conflict.


How can I make a Django query for the first occurrence of a foreign key in a column?
Look over this recipe from the ActiveState archives and see if it looks like the kind of stuff you like:.
django admin: company branches must manage only their records across many models

Extract from linked recipe:

The simplest case where a metatype conflict happens is the following.


Why is Standard Input is not displayed as I type in Mac OS X Terminal application?
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.


Django popup box Error?? Running development web server
The question is: what is the metaclass of C ? Is it M_A or M_B ?.
Django: do I need to restart Apache when deploying?
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?".



85 out of 100 based on 85 user ratings 835 reviews