[Python-Dev] PEP: Ordered Class Definition Namespace

Eric Snow ericsnowcurrently at gmail.com
Tue Jun 7 17:20:02 EDT 2016


On Tue, Jun 7, 2016 at 12:30 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> On 7 June 2016 at 10:51, Eric Snow <ericsnowcurrently at gmail.com> wrote:
>> * ``__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.

I'm not clear on your point.  Decorators are applied after the class
has been created.  Hence they have no impact on the class's definition
order.  I'd expect __definition_order__ to strictly represent what
happened in the class body during definition, and not anything
afterward.

Certainly __definition_order__ might not align with __dict__ (or
dir()); we don't have any way to guarantee that it would, do we?  If
anything, the ability to diff __definition_order__ and __dict__ is a
positive, since it allows you to see changes on the class since it was
defined.

>
> 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).

I think both of those make __definition_order__ more complicated and
less useful.  As the PEP stands, folks can be confident in what
__definition_order__ represents.  What would you consider to be the
benefit of a mutable (or replaceable) __definition_order__ that
outweighs the benefit of a simpler definition of what's in it.

BTW, thanks for bringing this up. :)

-eric


More information about the Python-Dev mailing list