[Python-ideas] A (meta)class algebra

Nick Coghlan ncoghlan at gmail.com
Fri Feb 13 12:59:34 CET 2015


On 13 February 2015 at 20:55, Petr Viktorin <encukou at gmail.com> wrote:
> On Fri, Feb 13, 2015 at 5:43 AM, Martin Teichmann
>> This becomes even worse if you want to add a metaclass to a class.
>> Then you have to change every class that inherits from it. Now you
>> say that code with multiple metaclasses is a bad thing in general,
>> but I don't agree. There are very simple examples where it makes
>> a lot of sense. Say, you want to write a class with traits that
>> implements an abstract base class. Wouldn't it be cool if you could
>> write
>
> This is the one thing keeping metaclasses from being truly general –
> using them limits inheritance.
>
> The problem is that metaclasses are too powerful.

FWIW, that's why http://legacy.python.org/dev/peps/pep-0422/ exists -
it aims to standardise Zope's notion of "class initialisers" so you
can have definition time behaviour that gets inherited by subclasses
(unlike explicit class decorators), without running the risk of
introducing metaclass conflicts, and without making the metaclass
system even more complicated.

Then the cases that can be safely combined with other metaclasses
would merely stop using a custom metaclass on 3.5+, and instead rely
entirely on having an appropriate base class.

The last round of reviews (back before 3.4 was released) raised a
bunch of interesting questions/challenges, and then somebody (*cough*)
decided it would be a good idea to go spend more time helping with
PyPI ecosystem wrangling instead :)

Regards,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list