[Python-Dev] PEP: Ordered Class Definition Namespace
ericsnowcurrently at gmail.com
Tue Jun 7 14:51:34 EDT 2016
On Tue, Jun 7, 2016 at 11:36 AM, Émanuel Barry <vgr255 at live.ca> wrote:
>> From: Eric Snow
>> * "dunder" attributes (e.g. ``__init__``, ``__module__``) are ignored
> What does this imply? If I define some __dunder__ methods, will they simply
> not be present in __definition_order__? What if I want to keep the order of
> those? While keeping the order of these might be meaningless in most cases,
> I don't think there's really a huge problem in doing so. Maybe I'm
> overthinking it.
"dunder" names (not just methods) will not be present in
__definition_order__. I'll add an explanation to the PEP. The gist
of it is that they are reserved for use by the interpreter and will
always clutter up __definition_order__. Since needing dunder names
included in __definition_order__ would be rather exceptional, and
there are other options available, leaving them out by default is a
matter of practicality.
>> * ``__definition_order__`` is a tuple
>> * ``__definition_order__`` is a read-only attribute
>> * ``__definition_order__`` is always set:
>> * if ``__definition_order__`` is defined in the class body then it
>> is used
>> * types that do not have a class definition (e.g. builtins) have
>> their ``__definition_order__`` set to ``None``
>> * types for which `__prepare__()`` returned something other than
>> ``OrderedDict`` (or a subclass) have their ``__definition_order__``
>> set to ``None``
> I would probably like a ``type.definition_order`` method, for which the
> return value is bound to __definition_order__ when the class is created
> (much like the link between ``type.mro`` and ``cls.__mro__``.
What is the value of type.definition_order()? If you need a mutable
copy then pass __definition_order__ to list().
> I'm not sure if setting the attribute to None is a good idea; I'd have it as
> an empty tuple. Then again I tend to overthink a lot.
None indicates that there is no order. An empty tuple indicates that
there were no attributes.
>> __definition_order__ = tuple(k for k in locals()
>> if (!k.startswith('__') or
> Mixing up C and Python syntax here.
nice catch :)
> I'm +1 on the whole idea (one of my common uses of metaclasses was to keep
> the definition order *somewhere*). Thank you for doing that!
More information about the Python-Dev