[Python-Dev] PEP 520: Ordered Class Definition Namespace

Nick Coghlan ncoghlan at gmail.com
Fri Jun 17 21:32:36 EDT 2016


On 7 June 2016 at 17:50, Eric Snow <ericsnowcurrently at gmail.com> wrote:
> Why is __definition_order__ 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, ``__definition_order__`` preserves this one bit of info
> from the class body so that it is universally available.

The discussion in the PEP 487 thread made me realise that I'd like to
see a discussion in PEP 520 regarding whether or not to define
__definition_order__ for builtin types initialised via PyType_Ready or
created via PyType_FromSpec in addition to defining it for types
created via the class statement or types.new_class().

For static types, PyType_Ready could potentially set it based on
tp_members, tp_methods & tp_getset (see
https://docs.python.org/3/c-api/typeobj.html )
Similarly, PyType_FromSpec could potentially set it based on the
contents of Py_tp_members, Py_tp_methods and Py_tp_getset slot
definitions

Having definition order support in both types.new_class() and builtin
types would also make it clear why we can't rely purely on the
compiler to provide the necessary ordering information - in both of
those cases, the Python compiler isn't directly involved in the type
creation process.

Cheers,
Nick.

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


More information about the Python-Dev mailing list