[Python-ideas] A (meta)class algebra

Thomas Kluyver thomas at kluyver.me.uk
Fri Feb 13 19:27:15 CET 2015


On 12 February 2015 at 20:43, Martin Teichmann <lkb.teichmann at gmail.com>
wrote:

> class ConsoleWidget(MetaQObjectHasTraits('NewBase',
> (LoggingConfigurable, QtGui.QWidget), {})):
>
> which, in my opinion, is hard to read and grasp. Everyone has to
> be taught on how to use those metaclass mixers.
>

Yes, it's hard to read, but it indicates that there's something complex
going on. Your proposal is cleaner, but to my mind it's just sweeping under
the carpet the fact that multiple metaclasses are in use. To fully
understand the code, you would still need to get a headache understanding
how multiple metaclasses interact, but you would first have to notice that
one metaclass is able to combine itself with another, and that there is
another one to combine with.


> I'm completely with you arguing that inheritance is overrated and
> composition should be used more often. Yet, I actually think that
> the above example is actually not such a bad idea, why should a
> QWidget not be LoggingConfigurable, and have traits?
>

It's not in itself a ridiculous idea, but it's bad practice precisely
because multiple inheritance is tricky. If inheritance means 'is a', it's
hard to be two completely different kinds of thing at the same time -
especially when both of those things rely on advanced features of Python.
If I was writing that code from scratch, I would try very hard to make it
such that Configurable objects *have* QObjects, rather than being QObjects.

Moreover, I don't think you can overcome this difficulty by adding any new
features to inheritance or metaclasses in Python. I doubt you could
overcome it even if you could redesign multiple inheritance with a blank
slate, but perhaps that is possible.

Given that I think the intersection of multiple inheritance and metaclasses
will always be horrible to understand, I think anything that makes it
easier to get into that situation is a bad idea. I'd support something that
makes it easier to understand such situations when they do exist, but I
think your proposal would make it harder, not easier.

Thomas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150213/487d1e30/attachment-0001.html>


More information about the Python-ideas mailing list