[Python-ideas] An even simpler customization of class creation
Nick Coghlan
ncoghlan at gmail.com
Mon Mar 2 13:45:22 CET 2015
On 28 February 2015 at 02:56, Martin Teichmann <lkb.teichmann at gmail.com> wrote:
> Connection to other PEP
> =======================
>
> This is a competing proposal to PEP 422 by Nick Coughlan and Daniel Urban.
> It shares both most of the PEP text and proposed code, but has major
> differences in how to achieve its goals.
I've now withdrawn PEP 422 in favour of this proposal:
https://hg.python.org/peps/rev/4db0c1a3e363 (main site hadn't updated
at time of writing, but it should propagate before too long)
> Proposal
> ========
>
> This PEP proposes that a new mechanism to customise class creation be
> added to Python 3.5 that meets the following criteria:
>
> 1. Integrates nicely with class inheritance structures (including mixins and
> multiple inheritance),
> 2. Integrates nicely with the implicit ``__class__`` reference and
> zero-argument ``super()`` syntax introduced by PEP 3135,
> 3. Can be added to an existing base class without a significant risk of
> introducing backwards compatibility problems, and
> 4. Restores the ability for class namespaces to have some influence on the
> class creation process (above and beyond populating the namespace itself),
> but potentially without the full flexibility of the Python 2 style
> ``__metaclass__`` hook.
>
> Those goals can be achieved by adding two functionalities:
>
> 1. A ``__init_subclass__`` hook that initializes all subclasses of a
> given class, and
In writing the withdrawal notice for PEP 422, I realised there's a
case that needs to be more clearly specified here: the handling of
cases where the base class already defines the hook. Since object has
a default implementation, we should be able formally define this as
the type creation machinery calling:
super(cls, cls).__init_subclass__(ns, **kwargs)
Rather than:
cls.__init_subclass__(ns, **kwargs)
We may also want to consider defining object.__init_subclass__ as
accepting "cls, ns, *, metaclass=type" so we don't need to implicitly
pop metaclass from the kwargs dict when it gets specified.
Regards,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list