[Python-Dev] PEP: Ordered Class Definition Namespace

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 ``__definition_order__`` attribute on each class
> * "dunder" attributes (e.g. ``__init__``, ``__module__``) are ignored
> * ``__definition_order__`` is a tuple
> * ``__definition_order__`` 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).


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

More information about the Python-Dev mailing list