[Python-ideas] Simpler Customization of Class Creation - PEP 487

Andrew Barnert abarnert at yahoo.com
Fri Feb 5 16:53:13 EST 2016


Two small things.

On Friday, February 5, 2016 1:21 PM, Martin Teichmann <lkb.teichmann at gmail.com> wrote:



> Most metaclasses, however, serve only some of the following three
> purposes: a) run some code after a class is created b) initialize descriptors
> of a class or c) keep the order in which class attributes have been defined.

How is c) not served by just defining a __prepare__ method that returns an OrderedDict? That seems easier to use than a separate dict and __attribute_order__ tuple. You later mention that many people already do this, but don't explain what's wrong with it or why your idea is better. It's not like a one-line __prepare__ is difficult to write, or like OrderedDict is too slow (especially in 3.6), so you're just adding a cost (less convenient to use) for no benefit that I can see.

Also:

> A path of introduction into Python
> ==================================
> 
> Most of the benefits of this PEP can already be implemented using
> a simple metaclass. For the ``__init_subclass__`` hook this works
> all the way down to Python 2.7, while the attribute order needs Python 3.0
> to work. Such a class has been `uploaded to PyPI`_.
>
> The only drawback of such a metaclass are the mentioned problems with
> metaclasses and multiple inheritance. 

It sounds like you're trying to have it both ways here: it seems like if your proposal really is better because it works with Python 2.7 rather than just 3.x, then your proposal being in the stdlib is probably a bad idea, because it will mislead rather than help people trying to write version-straddling code.


More information about the Python-ideas mailing list