Skip to content

Commit 2c76730

Browse files
Focus PEP 520 on __definition_order__. (#27)
1 parent 6124527 commit 2c76730

File tree

1 file changed

+37
-27
lines changed

1 file changed

+37
-27
lines changed

pep-0520.txt

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PEP: 520
2-
Title: Ordered Class Definition Namespace
2+
Title: Preserving Class Attribute Definition Order
33
Version: $Revision$
44
Last-Modified: $Date$
55
Author: Eric Snow <[email protected]>
@@ -8,7 +8,7 @@ Type: Standards Track
88
Content-Type: text/x-rst
99
Created: 7-Jun-2016
1010
Python-Version: 3.6
11-
Post-History: 7-Jun-2016, 11-Jun-2016
11+
Post-History: 7-Jun-2016, 11-Jun-2016, 20-Jun-2016
1212

1313

1414
Abstract
@@ -20,44 +20,50 @@ namespace is copied into new ``dict`` and the original definition
2020
namespace is discarded. The new copy is stored away as the class's
2121
namespace and is exposed as ``__dict__`` through a read-only proxy.
2222

23-
This PEP changes the default class definition namespace to ``OrderedDict``.
24-
The long-lived class namespace (``__dict__``) will remain a ``dict``.
25-
Furthermore, the order in which the attributes are defined in each class
26-
body will now be preserved in the ``__definition_order__`` attribute of
27-
the class. This allows introspection of the original definition order,
23+
This PEP preserves the order in which the attributes in the definition
24+
namespace were added to it, before that namespace is discarded. This
25+
means it reflects the definition order of the class body. That order
26+
will now be preserved in the ``__definition_order__`` attribute of the
27+
class. This allows introspection of the original definition order,
2828
e.g. by class decorators.
2929

30+
Additionally, this PEP changes the default class definition namespace
31+
to ``OrderedDict``. The long-lived class namespace (``__dict__``) will
32+
remain a ``dict``.
33+
3034

3135
Motivation
3236
==========
3337

34-
Currently the namespace used during execution of a class body defaults
35-
to ``dict``. If the metaclass defines ``__prepare__()`` then the result
36-
of calling it is used. Thus, before this PEP, if you needed your class
37-
definition namespace to be ``OrderedDict`` you had to use a metaclass.
38-
39-
Metaclasses introduce an extra level of complexity to code and in some
40-
cases (e.g. conflicts) are a problem. So reducing the need for them is
41-
worth doing when the opportunity presents itself. Given that we now have
42-
a C implementation of ``OrderedDict`` and that ``OrderedDict`` is the
43-
common use case for ``__prepare__()``, we have such an opportunity by
44-
defaulting to ``OrderedDict``.
45-
46-
The usefulness of ``OrderedDict``-by-default is greatly increased if the
47-
definition order is directly introspectable on classes afterward,
48-
particularly by code that is independent of the original class definition.
38+
Currently Python does not preserve the order in which attributes are
39+
added to the class definition namespace. The namespace used during
40+
execution of a class body defaults to ``dict``. If the metaclass
41+
defines ``__prepare__()`` then the result of calling it is used. Thus,
42+
before this PEP, to access your class definition namespace you must
43+
use ``OrderedDict`` along with a metaclass. Then you must preserve the
44+
definition order (from the ``OrderedDict``) yourself. This has a
45+
couple of problems.
46+
47+
First, it requires the use of a metaclass. Metaclasses introduce an
48+
extra level of complexity to code and in some cases (e.g. conflicts)
49+
are a problem. So reducing the need for them is worth doing when the
50+
opportunity presents itself. PEP 422 and PEP 487 discuss this at
51+
length. Given that we now have a C implementation of ``OrderedDict``
52+
and that ``OrderedDict`` is the common use case for ``__prepare__()``,
53+
we have such an opportunity by defaulting to ``OrderedDict``.
54+
55+
Second, only classes that opt in to using the ``OrderedDict``-based
56+
metaclass will have access to the definition order. This is problematic
57+
for cases where universal access to the definition order is important.
4958
One of the original motivating use cases for this PEP is generic class
5059
decorators that make use of the definition order.
5160

52-
Changing the default class definition namespace has been discussed a
53-
number of times, including on the mailing lists and in PEP 422 and
54-
PEP 487 (see the References section below).
55-
5661

5762
Specification
5863
=============
5964

60-
* the default class *definition* namespace is now ``OrderdDict``
65+
Part 1:
66+
6167
* the order in which class attributes are defined is preserved in the
6268
new ``__definition_order__`` attribute on each class
6369
* "dunder" attributes (e.g. ``__init__``, ``__module__``) are ignored
@@ -74,6 +80,10 @@ Specification
7480
``OrderedDict`` (or a subclass) have their ``__definition_order__``
7581
set to ``None`` (except where #1 applies)
7682

83+
Part 2:
84+
85+
* the default class *definition* namespace is now ``OrderdDict``
86+
7787
The following code demonstrates roughly equivalent semantics for the
7888
default behavior::
7989

0 commit comments

Comments
 (0)