2012-06-05 08:09:20 -04:00
|
|
|
PEP: 422
|
2013-03-04 10:54:19 -05:00
|
|
|
Title: Simpler customisation of class creation
|
2012-06-05 08:09:20 -04:00
|
|
|
Version: $Revision$
|
|
|
|
Last-Modified: $Date$
|
2013-02-10 07:13:58 -05:00
|
|
|
Author: Nick Coghlan <ncoghlan@gmail.com>,
|
|
|
|
Daniel Urban <urban.dani+py@gmail.com>
|
2015-03-02 07:32:31 -05:00
|
|
|
Status: Withdrawn
|
2012-06-05 08:09:20 -04:00
|
|
|
Type: Standards Track
|
|
|
|
Content-Type: text/x-rst
|
|
|
|
Created: 5-Jun-2012
|
2013-09-15 10:36:52 -04:00
|
|
|
Python-Version: 3.5
|
2013-03-04 10:54:19 -05:00
|
|
|
Post-History: 5-Jun-2012, 10-Feb-2013
|
2012-06-05 08:09:20 -04:00
|
|
|
|
|
|
|
|
|
|
|
Abstract
|
|
|
|
========
|
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
Currently, customising class creation requires the use of a custom metaclass.
|
|
|
|
This custom metaclass then persists for the entire lifecycle of the class,
|
|
|
|
creating the potential for spurious metaclass conflicts.
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
This PEP proposes to instead support a wide range of customisation
|
|
|
|
scenarios through a new ``namespace`` parameter in the class header, and
|
2015-02-22 09:47:12 -05:00
|
|
|
a new ``__autodecorate__`` hook in the class body.
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2015-02-22 09:47:12 -05:00
|
|
|
The new mechanism should be easier to understand and use than
|
2013-03-04 10:54:19 -05:00
|
|
|
implementing a custom metaclass, and thus should provide a gentler
|
|
|
|
introduction to the full power Python's metaclass machinery.
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2015-02-22 10:14:58 -05:00
|
|
|
|
2015-03-02 07:32:31 -05:00
|
|
|
PEP Withdrawal
|
|
|
|
==============
|
2013-09-15 10:36:52 -04:00
|
|
|
|
2015-03-02 07:32:31 -05:00
|
|
|
This proposal has been withdrawn in favour of Martin Teichmann's proposal
|
|
|
|
in PEP 487, which achieves the same goals through a simpler, easier to use
|
|
|
|
``__init_subclass__`` hook that simply isn't invoked for the base class
|
|
|
|
that defines the hook.
|
2013-09-15 10:36:52 -04:00
|
|
|
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2012-06-06 07:40:04 -04:00
|
|
|
Background
|
|
|
|
==========
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2012-06-06 07:40:04 -04:00
|
|
|
For an already created class ``cls``, the term "metaclass" has a clear
|
|
|
|
meaning: it is the value of ``type(cls)``.
|
|
|
|
|
|
|
|
*During* class creation, it has another meaning: it is also used to refer to
|
|
|
|
the metaclass hint that may be provided as part of the class definition.
|
|
|
|
While in many cases these two meanings end up referring to one and the same
|
|
|
|
object, there are two situations where that is not the case:
|
|
|
|
|
2013-02-10 07:13:58 -05:00
|
|
|
* If the metaclass hint refers to an instance of ``type``, then it is
|
2012-06-06 07:40:04 -04:00
|
|
|
considered as a candidate metaclass along with the metaclasses of all of
|
|
|
|
the parents of the class being defined. If a more appropriate metaclass is
|
|
|
|
found amongst the candidates, then it will be used instead of the one
|
|
|
|
given in the metaclass hint.
|
|
|
|
* Otherwise, an explicit metaclass hint is assumed to be a factory function
|
|
|
|
and is called directly to create the class object. In this case, the final
|
|
|
|
metaclass will be determined by the factory function definition. In the
|
|
|
|
typical case (where the factory functions just calls ``type``, or, in
|
|
|
|
Python 3.3 or later, ``types.new_class``) the actual metaclass is then
|
|
|
|
determined based on the parent classes.
|
|
|
|
|
|
|
|
It is notable that only the actual metaclass is inherited - a factory
|
|
|
|
function used as a metaclass hook sees only the class currently being
|
|
|
|
defined, and is not invoked for any subclasses.
|
|
|
|
|
|
|
|
In Python 3, the metaclass hint is provided using the ``metaclass=Meta``
|
|
|
|
keyword syntax in the class header. This allows the ``__prepare__`` method
|
|
|
|
on the metaclass to be used to create the ``locals()`` namespace used during
|
|
|
|
execution of the class body (for example, specifying the use of
|
|
|
|
``collections.OrderedDict`` instead of a regular ``dict``).
|
|
|
|
|
|
|
|
In Python 2, there was no ``__prepare__`` method (that API was added for
|
|
|
|
Python 3 by PEP 3115). Instead, a class body could set the ``__metaclass__``
|
|
|
|
attribute, and the class creation process would extract that value from the
|
|
|
|
class namespace to use as the metaclass hint. There is `published code`_ that
|
|
|
|
makes use of this feature.
|
|
|
|
|
2012-06-07 08:08:41 -04:00
|
|
|
Another new feature in Python 3 is the zero-argument form of the ``super()``
|
|
|
|
builtin, introduced by PEP 3135. This feature uses an implicit ``__class__``
|
|
|
|
reference to the class being defined to replace the "by name" references
|
|
|
|
required in Python 2. Just as code invoked during execution of a Python 2
|
|
|
|
metaclass could not call methods that referenced the class by name (as the
|
|
|
|
name had not yet been bound in the containing scope), similarly, Python 3
|
|
|
|
metaclasses cannot call methods that rely on the implicit ``__class__``
|
|
|
|
reference (as it is not populated until after the metaclass has returned
|
2013-03-04 10:54:19 -05:00
|
|
|
control to the class creation machinery).
|
|
|
|
|
|
|
|
Finally, when a class uses a custom metaclass, it can pose additional
|
|
|
|
challenges to the use of multiple inheritance, as a new class cannot
|
|
|
|
inherit from parent classes with unrelated metaclasses. This means that
|
|
|
|
it is impossible to add a metaclass to an already published class: such
|
|
|
|
an addition is a backwards incompatible change due to the risk of metaclass
|
|
|
|
conflicts.
|
2012-06-07 08:08:41 -04:00
|
|
|
|
2012-06-06 07:40:04 -04:00
|
|
|
|
|
|
|
Proposal
|
|
|
|
========
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
This PEP proposes that a new mechanism to customise class creation be
|
|
|
|
added to Python 3.4 that meets the following criteria:
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
1. Integrates nicely with class inheritance structures (including mixins and
|
2012-06-06 07:44:01 -04:00
|
|
|
multiple inheritance)
|
2013-03-04 10:54:19 -05:00
|
|
|
2. Integrates nicely with the implicit ``__class__`` reference and
|
2012-06-06 07:44:01 -04:00
|
|
|
zero-argument ``super()`` syntax introduced by PEP 3135
|
2013-03-04 10:54:19 -05:00
|
|
|
3. Can be added to an existing base class without a significant risk of
|
2012-06-06 07:44:01 -04:00
|
|
|
introducing backwards compatibility problems
|
2013-03-04 10:54:19 -05:00
|
|
|
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
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2015-02-22 09:47:12 -05:00
|
|
|
One mechanism that can achieve this goal is to add a new implicit class
|
|
|
|
decoration hook, modelled directly on the existing explicit class
|
|
|
|
decorators, but defined in the class body or in a parent class, rather than
|
|
|
|
being part of the class definition header.
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2012-06-06 07:40:04 -04:00
|
|
|
Specifically, it is proposed that class definitions be able to provide a
|
|
|
|
class initialisation hook as follows::
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2012-06-06 07:40:04 -04:00
|
|
|
class Example:
|
2015-02-22 09:47:12 -05:00
|
|
|
def __autodecorate__(cls):
|
2012-06-06 07:40:04 -04:00
|
|
|
# This is invoked after the class is created, but before any
|
|
|
|
# explicit decorators are called
|
|
|
|
# The usual super() mechanisms are used to correctly support
|
2013-02-10 07:13:58 -05:00
|
|
|
# multiple inheritance. The class decorator style signature helps
|
2012-06-06 07:45:26 -04:00
|
|
|
# ensure that invoking the parent class is as simple as possible.
|
2015-02-23 06:34:36 -05:00
|
|
|
cls = super().__autodecorate__()
|
|
|
|
return cls
|
|
|
|
|
|
|
|
To simplify the cooperative multiple inheritance case, ``object`` will gain
|
2016-05-03 04:35:10 -04:00
|
|
|
a default implementation of the hook that returns the class unmodified::
|
2015-02-23 06:34:36 -05:00
|
|
|
|
|
|
|
class object:
|
|
|
|
def __autodecorate__(cls):
|
|
|
|
return cls
|
|
|
|
|
|
|
|
If a metaclass wishes to block implicit class decoration for some reason, it
|
|
|
|
must arrange for ``cls.__autodecorate__`` to trigger ``AttributeError``.
|
2012-06-06 07:40:04 -04:00
|
|
|
|
|
|
|
If present on the created object, this new hook will be called by the class
|
|
|
|
creation machinery *after* the ``__class__`` reference has been initialised.
|
|
|
|
For ``types.new_class()``, it will be called as the last step before
|
2015-02-22 09:47:12 -05:00
|
|
|
returning the created class object. ``__autodecorate__`` is implicitly
|
2013-03-04 10:54:19 -05:00
|
|
|
converted to a class method when the class is created (prior to the hook
|
|
|
|
being invoked).
|
2012-06-06 07:40:04 -04:00
|
|
|
|
2015-02-22 09:47:12 -05:00
|
|
|
Note, that when ``__autodecorate__`` is called, the name of the class is not
|
2013-03-04 10:54:19 -05:00
|
|
|
yet bound to the new class object. As a consequence, the two argument form
|
2013-02-10 07:13:58 -05:00
|
|
|
of ``super()`` cannot be used to call methods (e.g., ``super(Example, cls)``
|
|
|
|
wouldn't work in the example above). However, the zero argument form of
|
|
|
|
``super()`` works as expected, since the ``__class__`` reference is already
|
|
|
|
initialised.
|
|
|
|
|
2012-06-07 08:08:41 -04:00
|
|
|
This general proposal is not a new idea (it was first suggested for
|
|
|
|
inclusion in the language definition `more than 10 years ago`_, and a
|
|
|
|
similar mechanism has long been supported by `Zope's ExtensionClass`_),
|
2013-02-10 07:13:58 -05:00
|
|
|
but the situation has changed sufficiently in recent years that
|
2015-02-23 06:34:36 -05:00
|
|
|
the idea is worth reconsidering for inclusion as a native language feature.
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2013-03-05 07:34:43 -05:00
|
|
|
In addition, the introduction of the metaclass ``__prepare__`` method in PEP
|
2013-03-04 10:54:19 -05:00
|
|
|
3115 allows a further enhancement that was not possible in Python 2: this
|
2013-03-05 07:22:20 -05:00
|
|
|
PEP also proposes that ``type.__prepare__`` be updated to accept a factory
|
|
|
|
function as a ``namespace`` keyword-only argument. If present, the value
|
|
|
|
provided as the ``namespace`` argument will be called without arguments
|
|
|
|
to create the result of ``type.__prepare__`` instead of using a freshly
|
|
|
|
created dictionary instance. For example, the following will use
|
2013-03-05 07:32:26 -05:00
|
|
|
an ordered dictionary as the class namespace::
|
2013-03-04 10:54:19 -05:00
|
|
|
|
2013-03-05 07:22:20 -05:00
|
|
|
class OrderedExample(namespace=collections.OrderedDict):
|
2015-02-22 09:47:12 -05:00
|
|
|
def __autodecorate__(cls):
|
2013-03-04 10:54:19 -05:00
|
|
|
# cls.__dict__ is still a read-only proxy to the class namespace,
|
2013-03-05 07:22:20 -05:00
|
|
|
# but the underlying storage is an OrderedDict instance
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
This PEP, along with the existing ability to use __prepare__ to share a
|
|
|
|
single namespace amongst multiple class objects, highlights a possible
|
|
|
|
issue with the attribute lookup caching: when the underlying mapping is
|
|
|
|
updated by other means, the attribute lookup cache is not invalidated
|
|
|
|
correctly (this is a key part of the reason class ``__dict__`` attributes
|
|
|
|
produce a read-only view of the underlying storage).
|
|
|
|
|
|
|
|
Since the optimisation provided by that cache is highly desirable,
|
|
|
|
the use of a preexisting namespace as the class namespace may need to
|
|
|
|
be declared as officially unsupported (since the observed behaviour is
|
|
|
|
rather strange when the caches get out of sync).
|
2013-03-04 10:54:19 -05:00
|
|
|
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2012-06-06 07:40:04 -04:00
|
|
|
Key Benefits
|
|
|
|
============
|
2012-06-05 08:09:20 -04:00
|
|
|
|
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
Easier use of custom namespaces for a class
|
|
|
|
-------------------------------------------
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
Currently, to use a different type (such as ``collections.OrderedDict``) for
|
|
|
|
a class namespace, or to use a pre-populated namespace, it is necessary to
|
|
|
|
write and use a custom metaclass. With this PEP, using a custom namespace
|
2013-03-05 07:22:20 -05:00
|
|
|
becomes as simple as specifying an appropriate factory function in the
|
|
|
|
class header.
|
2012-06-05 08:09:20 -04:00
|
|
|
|
|
|
|
|
2012-06-06 07:40:04 -04:00
|
|
|
Easier inheritance of definition time behaviour
|
|
|
|
-----------------------------------------------
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2012-06-07 08:08:41 -04:00
|
|
|
Understanding Python's metaclasses requires a deep understanding of
|
2012-06-06 07:40:04 -04:00
|
|
|
the type system and the class construction process. This is legitimately
|
2012-06-07 08:08:41 -04:00
|
|
|
seen as challenging, due to the need to keep multiple moving parts (the code,
|
2012-06-06 07:40:04 -04:00
|
|
|
the metaclass hint, the actual metaclass, the class object, instances of the
|
2012-06-07 08:08:41 -04:00
|
|
|
class object) clearly distinct in your mind. Even when you know the rules,
|
|
|
|
it's still easy to make a mistake if you're not being extremely careful.
|
|
|
|
An earlier version of this PEP actually included such a mistake: it
|
2013-02-10 07:13:58 -05:00
|
|
|
stated "subclass of type" for a constraint that is actually "instance of
|
2012-06-07 08:08:41 -04:00
|
|
|
type".
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2015-02-22 10:14:58 -05:00
|
|
|
Understanding the proposed implicit class decoration hook only requires
|
2012-06-07 08:08:41 -04:00
|
|
|
understanding decorators and ordinary method inheritance, which isn't
|
|
|
|
quite as daunting a task. The new hook provides a more gradual path
|
|
|
|
towards understanding all of the phases involved in the class definition
|
|
|
|
process.
|
2012-06-05 08:09:20 -04:00
|
|
|
|
|
|
|
|
2012-06-06 07:40:04 -04:00
|
|
|
Reduced chance of metaclass conflicts
|
|
|
|
-------------------------------------
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2012-06-06 07:40:04 -04:00
|
|
|
One of the big issues that makes library authors reluctant to use metaclasses
|
2012-06-07 08:08:41 -04:00
|
|
|
(even when they would be appropriate) is the risk of metaclass conflicts.
|
2012-06-06 07:40:04 -04:00
|
|
|
These occur whenever two unrelated metaclasses are used by the desired
|
|
|
|
parents of a class definition. This risk also makes it very difficult to
|
|
|
|
*add* a metaclass to a class that has previously been published without one.
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2015-02-22 09:47:12 -05:00
|
|
|
By contrast, adding an ``__autodecorate__`` method to an existing type poses
|
2012-06-06 07:40:04 -04:00
|
|
|
a similar level of risk to adding an ``__init__`` method: technically, there
|
|
|
|
is a risk of breaking poorly implemented subclasses, but when that occurs,
|
|
|
|
it is recognised as a bug in the subclass rather than the library author
|
|
|
|
breaching backwards compatibility guarantees. In fact, due to the constrained
|
2015-02-22 09:47:12 -05:00
|
|
|
signature of ``__autodecorate__``, the risk in this case is actually even
|
2012-06-07 08:08:41 -04:00
|
|
|
lower than in the case of ``__init__``.
|
2012-06-05 08:09:20 -04:00
|
|
|
|
|
|
|
|
2012-06-07 08:08:41 -04:00
|
|
|
Integrates cleanly with \PEP 3135
|
|
|
|
---------------------------------
|
2012-06-06 07:40:04 -04:00
|
|
|
|
|
|
|
Unlike code that runs as part of the metaclass, code that runs as part of
|
|
|
|
the new hook will be able to freely invoke class methods that rely on the
|
|
|
|
implicit ``__class__`` reference introduced by PEP 3135, including methods
|
|
|
|
that use the zero argument form of ``super()``.
|
|
|
|
|
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
Replaces many use cases for dynamic setting of ``__metaclass__``
|
|
|
|
-----------------------------------------------------------------
|
2012-06-06 07:40:04 -04:00
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
For use cases that don't involve completely replacing the defined class,
|
|
|
|
Python 2 code that dynamically set ``__metaclass__`` can now dynamically
|
2015-02-22 09:47:12 -05:00
|
|
|
set ``__autodecorate__`` instead. For more advanced use cases, introduction of
|
2013-03-04 10:54:19 -05:00
|
|
|
an explicit metaclass (possibly made available as a required base class) will
|
|
|
|
still be necessary in order to support Python 3.
|
2012-06-06 07:40:04 -04:00
|
|
|
|
|
|
|
|
2015-02-22 09:47:12 -05:00
|
|
|
Design Notes
|
|
|
|
============
|
|
|
|
|
|
|
|
|
|
|
|
Determining if the class being decorated is the base class
|
|
|
|
----------------------------------------------------------
|
|
|
|
|
|
|
|
In the body of an ``__autodecorate__`` method, as in any other class method,
|
|
|
|
``__class__`` will be bound to the class declaring the method, while the
|
|
|
|
value passed in may be a subclass.
|
|
|
|
|
|
|
|
This makes it relatively straightforward to skip processing the base class
|
|
|
|
if necessary::
|
|
|
|
|
|
|
|
class Example:
|
|
|
|
def __autodecorate__(cls):
|
2015-02-23 06:34:36 -05:00
|
|
|
cls = super().__autodecorate__()
|
2015-02-22 09:47:12 -05:00
|
|
|
# Don't process the base class
|
|
|
|
if cls is __class__:
|
|
|
|
return
|
|
|
|
# Process subclasses here
|
2015-02-22 10:14:58 -05:00
|
|
|
...
|
2015-02-22 09:47:12 -05:00
|
|
|
|
|
|
|
|
2015-02-23 06:34:36 -05:00
|
|
|
Replacing a class with a different kind of object
|
|
|
|
-------------------------------------------------
|
|
|
|
|
|
|
|
As an implicit decorator, ``__autodecorate__`` is able to relatively easily
|
|
|
|
replace the defined class with a different kind of object. Technically
|
|
|
|
custom metaclasses and even ``__new__`` methods can already do this
|
|
|
|
implicitly, but the decorator model makes such code much easier to understand
|
|
|
|
and implement.
|
|
|
|
|
2015-02-27 09:34:48 -05:00
|
|
|
::
|
|
|
|
|
2015-02-23 06:34:36 -05:00
|
|
|
class BuildDict:
|
|
|
|
def __autodecorate__(cls):
|
|
|
|
cls = super().__autodecorate__()
|
|
|
|
# Don't process the base class
|
|
|
|
if cls is __class__:
|
|
|
|
return
|
|
|
|
# Convert subclasses to ordinary dictionaries
|
|
|
|
return cls.__dict__.copy()
|
|
|
|
|
|
|
|
It's not clear why anyone would ever do this implicitly based on inheritance
|
|
|
|
rather than just using an explicit decorator, but the possibility seems worth
|
|
|
|
noting.
|
|
|
|
|
|
|
|
|
|
|
|
Open Questions
|
|
|
|
==============
|
|
|
|
|
|
|
|
Is the ``namespace`` concept worth the extra complexity?
|
|
|
|
--------------------------------------------------------
|
|
|
|
|
|
|
|
Unlike the new ``__autodecorate__`` hook the proposed ``namespace`` keyword
|
|
|
|
argument is not automatically inherited by subclasses. Given the way this
|
|
|
|
proposal is currently written , the only way to get a special namespace used
|
|
|
|
consistently in subclasses is still to write a custom metaclass with a
|
|
|
|
suitable ``__prepare__`` implementation.
|
|
|
|
|
|
|
|
Changing the custom namespace factory to also be inherited would
|
|
|
|
significantly increase the complexity of this proposal, and introduce a
|
|
|
|
number of the same potential base class conflict issues as arise with the
|
|
|
|
use of custom metaclasses.
|
|
|
|
|
|
|
|
Eric Snow has put forward a
|
|
|
|
`separate proposal <https://mail.python.org/pipermail/python-dev/2013-June/127103.html>`__
|
|
|
|
to instead make the execution namespace for class bodies an ordered dictionary
|
|
|
|
by default, and capture the class attribute definition order for future
|
|
|
|
reference as an attribute (e.g. ``__definition_order__``) on the class object.
|
|
|
|
|
|
|
|
Eric's suggested approach may be a better choice for a new default behaviour
|
|
|
|
for type that combines well with the proposed ``__autodecorate__`` hook,
|
|
|
|
leaving the more complex configurable namespace factory idea to a custom
|
|
|
|
metaclass like the one shown below.
|
|
|
|
|
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
New Ways of Using Classes
|
|
|
|
=========================
|
2012-06-06 07:40:04 -04:00
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
The new ``namespace`` keyword in the class header enables a number of
|
|
|
|
interesting options for controlling the way a class is initialised,
|
|
|
|
including some aspects of the object models of both Javascript and Ruby.
|
2012-06-06 07:40:04 -04:00
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
All of the examples below are actually possible today through the use of a
|
|
|
|
custom metaclass::
|
2012-06-06 07:40:04 -04:00
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
class CustomNamespace(type):
|
|
|
|
@classmethod
|
|
|
|
def __prepare__(meta, name, bases, *, namespace=None, **kwds):
|
|
|
|
parent_namespace = super().__prepare__(name, bases, **kwds)
|
2013-03-05 07:22:20 -05:00
|
|
|
return namespace() if namespace is not None else parent_namespace
|
2013-03-04 10:54:19 -05:00
|
|
|
def __new__(meta, name, bases, ns, *, namespace=None, **kwds):
|
|
|
|
return super().__new__(meta, name, bases, ns, **kwds)
|
|
|
|
def __init__(cls, name, bases, ns, *, namespace=None, **kwds):
|
|
|
|
return super().__init__(name, bases, ns, **kwds)
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
The advantage of implementing the new keyword directly in
|
2013-03-05 07:22:20 -05:00
|
|
|
``type.__prepare__`` is that the *only* persistent effect is then
|
2013-03-04 10:54:19 -05:00
|
|
|
the change in the underlying storage of the class attributes. The metaclass
|
|
|
|
of the class remains unchanged, eliminating many of the drawbacks
|
|
|
|
typically associated with these kinds of customisations.
|
2012-06-05 08:09:20 -04:00
|
|
|
|
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
Order preserving classes
|
|
|
|
------------------------
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
::
|
2013-03-04 10:57:51 -05:00
|
|
|
|
2013-03-05 07:22:20 -05:00
|
|
|
class OrderedClass(namespace=collections.OrderedDict):
|
2013-03-04 10:54:19 -05:00
|
|
|
a = 1
|
|
|
|
b = 2
|
|
|
|
c = 3
|
2012-06-06 07:40:04 -04:00
|
|
|
|
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
Prepopulated namespaces
|
|
|
|
-----------------------
|
2012-06-06 07:40:04 -04:00
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
::
|
2013-03-04 10:57:51 -05:00
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
seed_data = dict(a=1, b=2, c=3)
|
2013-03-05 07:22:20 -05:00
|
|
|
class PrepopulatedClass(namespace=seed_data.copy):
|
2013-03-04 10:54:19 -05:00
|
|
|
pass
|
2012-06-06 07:40:04 -04:00
|
|
|
|
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
Cloning a prototype class
|
|
|
|
-------------------------
|
2012-06-06 07:40:04 -04:00
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
::
|
2013-03-04 10:57:51 -05:00
|
|
|
|
2013-03-05 07:22:20 -05:00
|
|
|
class NewClass(namespace=Prototype.__dict__.copy):
|
2013-03-04 10:54:19 -05:00
|
|
|
pass
|
2012-06-06 07:40:04 -04:00
|
|
|
|
|
|
|
|
2013-03-05 07:22:20 -05:00
|
|
|
Extending a class
|
|
|
|
-----------------
|
2012-06-06 07:40:04 -04:00
|
|
|
|
2015-02-22 10:14:58 -05:00
|
|
|
.. note:: Just because the PEP makes it *possible* to do this relatively
|
2013-03-05 07:22:20 -05:00
|
|
|
cleanly doesn't mean anyone *should* do this!
|
2013-03-04 10:57:51 -05:00
|
|
|
|
2013-03-05 07:22:20 -05:00
|
|
|
::
|
2013-03-04 10:54:19 -05:00
|
|
|
|
2013-03-05 07:22:20 -05:00
|
|
|
from collections import MutableMapping
|
|
|
|
|
|
|
|
# The MutableMapping + dict combination should give something that
|
|
|
|
# generally behaves correctly as a mapping, while still being accepted
|
|
|
|
# as a class namespace
|
|
|
|
class ClassNamespace(MutableMapping, dict):
|
|
|
|
def __init__(self, cls):
|
|
|
|
self._cls = cls
|
|
|
|
def __len__(self):
|
|
|
|
return len(dir(self._cls))
|
|
|
|
def __iter__(self):
|
|
|
|
for attr in dir(self._cls):
|
|
|
|
yield attr
|
|
|
|
def __contains__(self, attr):
|
|
|
|
return hasattr(self._cls, attr)
|
|
|
|
def __getitem__(self, attr):
|
|
|
|
return getattr(self._cls, attr)
|
|
|
|
def __setitem__(self, attr, value):
|
|
|
|
setattr(self._cls, attr, value)
|
|
|
|
def __delitem__(self, attr):
|
|
|
|
delattr(self._cls, attr)
|
|
|
|
|
|
|
|
def extend(cls):
|
|
|
|
return lambda: ClassNamespace(cls)
|
|
|
|
|
|
|
|
class Example:
|
2013-03-04 10:54:19 -05:00
|
|
|
pass
|
|
|
|
|
2013-03-05 07:22:20 -05:00
|
|
|
class ExtendedExample(namespace=extend(Example)):
|
|
|
|
a = 1
|
|
|
|
b = 2
|
|
|
|
c = 3
|
|
|
|
|
|
|
|
>>> Example.a, Example.b, Example.c
|
|
|
|
(1, 2, 3)
|
|
|
|
|
2013-03-04 10:54:19 -05:00
|
|
|
|
|
|
|
Rejected Design Options
|
|
|
|
=======================
|
|
|
|
|
|
|
|
|
2015-02-22 09:47:12 -05:00
|
|
|
Calling ``__autodecorate__`` from ``type.__init__``
|
|
|
|
---------------------------------------------------
|
2012-06-07 08:08:41 -04:00
|
|
|
|
|
|
|
Calling the new hook automatically from ``type.__init__``, would achieve most
|
|
|
|
of the goals of this PEP. However, using that approach would mean that
|
2015-02-22 09:47:12 -05:00
|
|
|
``__autodecorate__`` implementations would be unable to call any methods that
|
2012-06-07 08:08:41 -04:00
|
|
|
relied on the ``__class__`` reference (or used the zero-argument form of
|
|
|
|
``super()``), and could not make use of those features themselves.
|
|
|
|
|
2015-02-22 10:14:58 -05:00
|
|
|
The current design instead ensures that the implicit decorator hook is able
|
|
|
|
to do anything an explicit decorator can do by running it after the initial
|
|
|
|
class creation is already complete.
|
2012-06-07 08:08:41 -04:00
|
|
|
|
2015-02-22 09:47:12 -05:00
|
|
|
Calling the automatic decoration hook ``__init_class__``
|
|
|
|
--------------------------------------------------------
|
|
|
|
|
|
|
|
Earlier versions of the PEP used the name ``__init_class__`` for the name
|
|
|
|
of the new hook. There were three significant problems with this name:
|
|
|
|
|
|
|
|
* it was hard to remember if the correct spelling was ``__init_class__`` or
|
|
|
|
``__class_init__``
|
|
|
|
* the use of "init" in the name suggested the signature should match that
|
|
|
|
of ``type.__init__``, which is not the case
|
|
|
|
* the use of "init" in the name suggested the method would be run as part
|
2015-02-22 10:14:58 -05:00
|
|
|
of initial class object creation, which is not the case
|
2015-02-22 09:47:12 -05:00
|
|
|
|
|
|
|
The new name ``__autodecorate__`` was chosen to make it clear that the new
|
|
|
|
initialisation hook is most usefully thought of as an implicitly invoked
|
|
|
|
class decorator, rather than as being like an ``__init__`` method.
|
|
|
|
|
|
|
|
|
|
|
|
Requiring an explicit decorator on ``__autodecorate__``
|
|
|
|
-------------------------------------------------------
|
2013-03-04 10:54:19 -05:00
|
|
|
|
|
|
|
Originally, this PEP required the explicit use of ``@classmethod`` on the
|
2015-02-22 09:47:12 -05:00
|
|
|
``__autodecorate__`` decorator. It was made implicit since there's no
|
2013-03-04 10:54:19 -05:00
|
|
|
sensible interpretation for leaving it out, and that case would need to be
|
|
|
|
detected anyway in order to give a useful error message.
|
|
|
|
|
|
|
|
This decision was reinforced after noticing that the user experience of
|
|
|
|
defining ``__prepare__`` and forgetting the ``@classmethod`` method
|
|
|
|
decorator is singularly incomprehensible (particularly since PEP 3115
|
|
|
|
documents it as an ordinary method, and the current documentation doesn't
|
|
|
|
explicitly say anything one way or the other).
|
|
|
|
|
|
|
|
|
2015-02-23 06:34:36 -05:00
|
|
|
Making ``__autodecorate__`` implicitly static, like ``__new__``
|
|
|
|
---------------------------------------------------------------
|
|
|
|
|
|
|
|
While it accepts the class to be instantiated as the first argument,
|
|
|
|
``__new__`` is actually implicitly treated as a static method rather than
|
|
|
|
as a class method. This allows it to be readily extracted from its
|
|
|
|
defining class and called directly on a subclass, rather than being
|
|
|
|
coupled to the class object it is retrieved from.
|
|
|
|
|
|
|
|
Such behaviour initially appears to be potentially useful for the
|
|
|
|
new ``__autodecorate__`` hook, as it would allow ``__autodecorate__``
|
|
|
|
methods to readily be used as explicit decorators on other classes.
|
|
|
|
|
|
|
|
However, that apparent support would be an illusion as it would only work
|
|
|
|
correctly if invoked on a subclass, in which case the method can just as
|
|
|
|
readily be retrieved from the subclass and called that way. Unlike
|
|
|
|
``__new__``, there's no issue with potentially changing method signatures at
|
|
|
|
different points in the inheritance chain.
|
|
|
|
|
|
|
|
|
2013-03-05 07:22:20 -05:00
|
|
|
Passing in the namespace directly rather than a factory function
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
|
|
|
At one point, this PEP proposed that the class namespace be passed
|
|
|
|
directly as a keyword argument, rather than passing a factory function.
|
|
|
|
However, this encourages an unsupported behaviour (that is, passing the
|
|
|
|
same namespace to multiple classes, or retaining direct write access
|
|
|
|
to a mapping used as a class namespace), so the API was switched to
|
|
|
|
the factory function version.
|
|
|
|
|
|
|
|
|
2013-02-10 06:47:22 -05:00
|
|
|
Reference Implementation
|
|
|
|
========================
|
|
|
|
|
2015-02-22 09:47:12 -05:00
|
|
|
A reference implementation for ``__autodecorate__`` has been posted to the
|
2015-02-23 06:34:36 -05:00
|
|
|
`issue tracker`_. It uses the original ``__init_class__`` naming. does not yet
|
|
|
|
allow the implicit decorator to replace the class with a different object and
|
|
|
|
does not implement the suggested ``namespace`` parameter for
|
|
|
|
``type.__prepare__``.
|
2013-03-04 10:54:19 -05:00
|
|
|
|
|
|
|
TODO
|
|
|
|
====
|
2013-02-10 06:47:22 -05:00
|
|
|
|
2017-06-11 15:02:39 -04:00
|
|
|
* address the 5 points in https://mail.python.org/pipermail/python-dev/2013-February/123970.html
|
2013-02-10 06:47:22 -05:00
|
|
|
|
2012-06-05 08:09:20 -04:00
|
|
|
References
|
|
|
|
==========
|
|
|
|
|
2012-06-06 07:40:04 -04:00
|
|
|
.. _published code:
|
2017-06-11 15:02:39 -04:00
|
|
|
https://mail.python.org/pipermail/python-dev/2012-June/119878.html
|
2012-06-06 07:40:04 -04:00
|
|
|
|
|
|
|
.. _more than 10 years ago:
|
2017-06-11 15:02:39 -04:00
|
|
|
https://mail.python.org/pipermail/python-dev/2001-November/018651.html
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2012-06-07 08:08:41 -04:00
|
|
|
.. _Zope's ExtensionClass:
|
|
|
|
http://docs.zope.org/zope_secrets/extensionclass.html
|
2012-06-05 08:09:20 -04:00
|
|
|
|
2013-02-10 06:47:22 -05:00
|
|
|
.. _issue tracker:
|
|
|
|
http://bugs.python.org/issue17044
|
|
|
|
|
2012-06-05 08:09:20 -04:00
|
|
|
Copyright
|
|
|
|
=========
|
|
|
|
|
|
|
|
This document has been placed in the public domain.
|
|
|
|
|
2013-02-10 06:47:22 -05:00
|
|
|
|
2012-06-05 08:09:20 -04:00
|
|
|
..
|
|
|
|
Local Variables:
|
|
|
|
mode: indented-text
|
|
|
|
indent-tabs-mode: nil
|
|
|
|
sentence-end-double-space: t
|
|
|
|
fill-column: 70
|
|
|
|
coding: utf-8
|
|
|
|
End:
|