[Python-Dev] PEP 520: Ordered Class Definition Namespace (original) (raw)
Ethan Furman ethan at stoneleaf.us
Tue Jun 7 21:20:38 EDT 2016
- Previous message (by thread): [Python-Dev] PEP 520: Ordered Class Definition Namespace
- Next message (by thread): [Python-Dev] PEP 520: Ordered Class Definition Namespace
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 06/07/2016 05:50 PM, Eric Snow wrote:
Overall +1. Some nits below.
Specification =============
3. types for which `_prepare_()
returned something other than_ _
OrderedDict(or a subclass) have their
_definitionorder__ _set to
None``
(unless ``__definition_order__`` is present in
the class dict either by virtue of being in the class body or
because the metaclass inserted it before calling
``type.__new__``)
definitionorder = tuple(k for k in locals() if (!k.startswith('') or_ !k.endswith('')))_
Still mixing C and Python! ;)
Why a tuple? ------------
Use of a tuple reflects the fact that we are exposing the order in which attributes on the class were defined. Since the definition is already complete by the time ``definitionorder`` is set, the_ content and order of the value won't be changing. Thus we use a type that communicates that state of immutability.
Why a read-only attribute? --------------------------
As with the use of tuple, making
_definitionorder_
a read-only attribute communicates the fact that the information it represents is complete. Since it represents the state of a particular one-time event (execution of the class definition body), allowing the value to be replaced would reduce confidence that the attribute corresponds to the original class body. If a use case for a writable (or mutable)_definitionorder_
arises, the restriction may be loosened later. Presently this seems unlikely and furthermore it is usually best to go immutable-by-default.
If definition_order is supposed to be immutable as well as read-only then we should convert non-tuples to tuples. No point in letting that user bug slip through.
Why ignore "dunder" names? --------------------------
Names starting and ending with "" are reserved for use by the_ interpreter. In practice they should not be relevant to the users of
_definitionorder_
. Instead, for early everyone they would only
s/early/nearly
Why is definitionorder even necessary? -------------------------------------------
Since the definition order is not preserved in
_dict_
, it would be lost once class definition execution completes. Classes could explicitly set the attribute as the last thing in the body. However, then independent decorators could only make use of classes that had done so. Instead,_definitionorder_
preserves this one bit of info from the class body so that it is universally available.
s/would be/is
--
Ethan
- Previous message (by thread): [Python-Dev] PEP 520: Ordered Class Definition Namespace
- Next message (by thread): [Python-Dev] PEP 520: Ordered Class Definition Namespace
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]