Minor clean up of PEP 520.

This commit is contained in:
Eric Snow 2016-06-07 20:09:27 -07:00
parent 06a5c815d7
commit 38a64f3338
1 changed files with 12 additions and 10 deletions

View File

@ -16,8 +16,9 @@ Abstract
This PEP changes the default class definition namespace to ``OrderedDict``. This PEP changes the default class definition namespace to ``OrderedDict``.
Furthermore, the order in which the attributes are defined in each class Furthermore, the order in which the attributes are defined in each class
body will now be preserved in ``type.__definition_order__``. This allows body will now be preserved in the ``__definition_order__`` attribute of
introspection of the original definition order, e.g. by class decorators. the class. This allows introspection of the original definition order,
e.g. by class decorators.
Note: just to be clear, this PEP is *not* about changing ``__dict__`` for Note: just to be clear, this PEP is *not* about changing ``__dict__`` for
classes to ``OrderedDict``. classes to ``OrderedDict``.
@ -62,9 +63,9 @@ Specification
1. if ``__definition_order__`` is defined in the class body then the 1. if ``__definition_order__`` is defined in the class body then the
value is used as-is, though the attribute will still be read-only value is used as-is, though the attribute will still be read-only
2. types that do not have a class definition (e.g. builtins) have 2. classes that do not have a class definition (e.g. builtins) have
their ``__definition_order__`` set to ``None`` their ``__definition_order__`` set to ``None``
3. types for which `__prepare__()`` returned something other than 3. classes for which `__prepare__()`` returned something other than
``OrderedDict`` (or a subclass) have their ``__definition_order__`` ``OrderedDict`` (or a subclass) have their ``__definition_order__``
set to ``None`` (except where #1 applies) set to ``None`` (except where #1 applies)
@ -78,8 +79,8 @@ The following code demonstrates roughly equivalent semantics::
ham = None ham = None
eggs = 5 eggs = 5
__definition_order__ = tuple(k for k in locals() __definition_order__ = tuple(k for k in locals()
if (!k.startswith('__') or if (not k.startswith('__') or
!k.endswith('__'))) not k.endswith('__')))
Note that [pep487_] proposes a similar solution, albeit as part of a Note that [pep487_] proposes a similar solution, albeit as part of a
broader proposal. broader proposal.
@ -169,14 +170,15 @@ The implementation is found in the tracker. [impl_]
Alternatives Alternatives
============ ============
type.__dict__ as OrderedDict <class>.__dict__ as OrderedDict
---------------------------- -------------------------------
Instead of storing the definition order in ``__definition_order__``, Instead of storing the definition order in ``__definition_order__``,
the now-ordered definition namespace could be copied into a new the now-ordered definition namespace could be copied into a new
``OrderedDict``. This would mostly provide the same semantics. ``OrderedDict``. This would then be used as the mapping proxied as
``__dict__``. Doing so would mostly provide the same semantics.
However, using ``OrderedDict`` for ``type,__dict__`` would obscure the However, using ``OrderedDict`` for ``__dict__`` would obscure the
relationship with the definition namespace, making it less useful. relationship with the definition namespace, making it less useful.
Additionally, doing this would require significant changes to the Additionally, doing this would require significant changes to the
semantics of the concrete ``dict`` C-API. semantics of the concrete ``dict`` C-API.