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

Eric Snow ericsnowcurrently at gmail.com
Tue Mar 1 19:03:58 EST 2016


On Fri, Feb 5, 2016 at 2:20 PM, Martin Teichmann
<lkb.teichmann at gmail.com> wrote:
> 3. an ``__attribute_order__`` tuple is left in the class in order to inspect
>    the order in which attributes were defined.
>
> [snip]
>
> The third part of the proposal is to leave a tuple called
> ``__attribute_order__`` in the class that contains the order in which
> the attributes were defined. This is a very common usecase, many
> libraries use an ``OrderedDict`` to store this order. This is a very
> simple way to achieve the same goal.
>
> [snip]
>
> PEP 422 defined a generic way to add arbitrary namespaces for class
> definitions. This approach is much more flexible than just leaving
> the definition order in a tuple. The ``__prepare__`` method in a metaclass
> supports exactly this behavior. But given that effectively
> the only use cases that could be found out in the wild were the
> ``OrderedDict`` way of determining the attribute order, it seemed
> reasonable to only support this special case.
>
> The metaclass described in this PEP has been designed to be very simple
> such that it could be reasonably made the default metaclass. This was
> especially important when designing the attribute order functionality:
> This was a highly demanded feature and has been enabled through the
> ``__prepare__`` method of metaclasses. This method can be abused in
> very weird ways, making it hard to correctly maintain this feature in
> CPython. This is why it has been proposed to deprecated this feature,
> and instead use ``OrderedDict`` as the standard namespace, supporting
> the most important feature while dropping most of the complexity. But
> this would have meant that ``OrderedDict`` becomes a language builtin
> like dict and set, and not just a standard library class. The choice
> of the ``__attribute_order__`` tuple is a much simpler solution to the
> problem.

Note that I've already been working on making OrderedDict the default
class definition namespace type. [1]  The patch to do so is actually
super simple (if I remember correctly).  This was actually one of my
main motivations for re-implementing OrderedDict in C (the other was
preserving the order of **kwargs).

Also note that we're thinking along the same lines regarding
__attribute_order__. :)  In the tracker issue I referenced you'll see
that I proposed roughly the same thing, calling it
__definition_order__ (which I prefer as the spelling).  I've brought
up the idea a couple of times on this list and got a positive response
(or so I remember <wink>) and was still planning on pursuing the
matter in time for Python 3.7.

My point is: this part of your proposal stands on its own and is
already being worked on (even if slowly).  FYI, one problem with
bundling related ideas in a PEP is that the concept of partial
acceptance isn't a natural part of the PEP process.  There have been a
few PEPs where part of the proposal made the cut but didn't go
anywhere because the whole PEP was rejected.

Anyway, keep the above in mind as it has bearing on your PEP.

-eric


[1] http://bugs.python.org/issue24254


More information about the Python-ideas mailing list