[Python-Dev] PEP: Ordered Class Definition Namespace (original) (raw)
Eric Snow ericsnowcurrently at gmail.com
Tue Jun 7 14:51:34 EDT 2016
- Previous message (by thread): [Python-Dev] PEP: Ordered Class Definition Namespace
- Next message (by thread): [Python-Dev] PEP: Ordered Class Definition Namespace
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 definitionorder? 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.
*
_definitionorder_
is a tuple *_definitionorder_
is a read-only attribute *_definitionorder_
is always set:* if
_definitionorder_
is defined in the class body then it is used * types that do not have a class definition (e.g. builtins) have their_definitionorder_
set toNone
* types for which `_prepare_()returned something other than_ _
OrderedDict(or a subclass) have their
_definitionorder__ _set to
None_ _I would probably like a
type.definitionordermethod, for which the_ _return value is bound to _definitionorder_ when the class is created_ _(much like the link between
type.mroand
cls._mro_``.
What is the value of type.definition_order()? If you need a mutable copy then pass definition_order to list().
Additionally 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.
definitionorder = tuple(k for k in locals() if (!k.startswith('') or_ !k.endswith('')))_ 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!
:)
-eric
- Previous message (by thread): [Python-Dev] PEP: Ordered Class Definition Namespace
- Next message (by thread): [Python-Dev] PEP: Ordered Class Definition Namespace
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]