[Python-Dev] PEP: Ordered Class Definition Namespace (original) (raw)

Nick Coghlan ncoghlan at gmail.com
Tue Jun 7 15:30:31 EDT 2016


On 7 June 2016 at 10:51, Eric Snow <ericsnowcurrently at gmail.com> wrote:

Specification =============

* the default class definition namespace is now OrderdDict * the order in which class attributes are defined is preserved in the new _definitionorder_ attribute on each class * "dunder" attributes (e.g. _init_, _module_) are ignored * _definitionorder_ is a tuple * _definitionorder_ is a read-only attribute

Thinking about the class decorator use case, I think this may need to be reconsidered, as class decorators may:

  1. Remove class attributes
  2. Add class attributes

This will then lead to definition_order getting out of sync with the current state of the class namespace.

One option for dealing with that would be to make type.setattr and type.delattr aware of definition_order, and have them replace the tuple with a new one as needed. If we did that, then the main question would be whether updating an existing attribute changed the definition order, and I'd be inclined to say "No" (to minimise the side effects of monkey-patching).

The main alternative would be to make definition_order writable, so the default behaviour would be for it to reflect the original class body, but decorators would be free to update it to reflect their changes, as well as to make other modifications (e.g. stripping out all callables from the list).

Cheers, Nick.

-- Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia



More information about the Python-Dev mailing list