[Python-ideas] An even simpler customization of class creation

Nick Coghlan ncoghlan at gmail.com
Sat Feb 28 18:10:21 CET 2015


On 28 February 2015 at 02:56, Martin Teichmann <lkb.teichmann at gmail.com> wrote:
> Hello everybody,
>
> recently, I started a discussion on how metaclasses could be
> improved. This lead to a major discussion about PEP 422,
> where I discussed some possible changes to PEP 422.
> Nick proposed I write a competing PEP, so that the community
> may decide.
>
> Here it comes. I propose that a class may define a classmethod
> called __init_subclass__ which initialized subclasses of this
> class, as a lightweight version of metaclasses.
>
> Compared to PEP 422, this proposal is much simpler to implement,
> but I do believe that it also makes more sense. PEP 422
> has a method called __autodecorate__ which acts on the class
> currently being generated. This is normally not what one wants:
> when writing a registering class, for example, one does not want
> to register the registering class itself. Most evident it become if
> one wants to introduce such a decorator as a mixin: certainly one
> does not want to tamper with the mixin class, but with the class
> it is mixed in!

Aye, I like this design, it just wasn't until I saw it written out
fully that it finally clicked why restricting it to subclasses let
both zero-argument super() and explicit namespace access to the
defining class both work. Getting the latter to also work is a
definite strength of your approach over mine.

> A lot of the text in my new PEP has been stolen from PEP 422.
> This is why I hesitated writing myself as the only author of my
> new PEP. On the other hand, I guess the original authors don't
> want to be bothered with my stuff, so I credited them in the
> body of my new PEP.

New PEP numbers can be a good idea for significant redesigns anyway,
otherwise people may end up having to qualify which version they're
talking about while deciding between two competing approaches.

In this case, unless someone comes up with an objection I've missed, I
think your __init_subclass__ design is just plain better than my
__autodecorate__ idea, and as you say, if we find a pressing need for
it, we could always add "__new_subclass__" later to get comparable
functionality. Assuming we don't hear any such objections, I'll
withdraw PEP 422 and we can focus on getting PEP 487 ready for
submission to python-dev.

That leaves my one concern being the "namespace" keyword parameter.
I'd started reconsidering that for PEP 422, and I think the argument
holds at least as strongly for your improved design:
https://www.python.org/dev/peps/pep-0422/#is-the-namespace-concept-worth-the-extra-complexity

Since speed usually isn't a critical concern for class definitions,
it's likely better to just make them ordered by default, which can be
its own PEP, rather than being part of this one.

Regards,
Nick.

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


More information about the Python-ideas mailing list