[Python-Dev] Classes with ordered namespaces (original) (raw)
Eric Snow ericsnowcurrently at gmail.com
Thu Jun 27 08:03:05 CEST 2013
- Previous message: [Python-Dev] Reminder: an oft-forgotten rule about docstring formatting (PEP 257)
- Next message: [Python-Dev] Classes with ordered namespaces
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
There are two relevant class namespaces in this proposal: definition namespace and dict. Currently both are dicts.
For class definition namespaces, I'd like to make the default an OrderedDict. With the implementation in issue16991, the change is trivial (basically 1-liners in 2 spots). This change would make it unnecessary to write a custom metaclass just for a prepare(). PEP 422 alleviates that problem somewhat. However, I expect OrderedDict is by far the most common type returned by prepare(), so having it be the default would proportionately reduce the need for people to write metaclasses or learn about the PEP 422 API. You may ask what is the point if they aren't using a metaclass. That leads to the other half of the proposal.
Once I have a class, I'd like to know the definition order without needing a metaclass. Unfortunately it's not as simple as using the C OrderedDict (issue16991 again) for tp_dict, etc. That change is actually pretty trivial. However, it causes problems because the concrete C API (PyDict_*) doesn't play nice with subclasses and the concrete API is used on class dict all over the place. The alternative I propose is to add definition_order or similar to classes. It would be a list of the names from the definition namespace, in definition order (making use of the new default there). Having a class's dict be ordered isn't important if the definition order is available somewhere. Again, using the C OrderedDict, the change to add definition_order is pretty trivial.
FWIW, Guido already expressed some approval[1].
-eric
[1] http://mail.python.org/pipermail/python-ideas/2013-February/019704.html
- Previous message: [Python-Dev] Reminder: an oft-forgotten rule about docstring formatting (PEP 257)
- Next message: [Python-Dev] Classes with ordered namespaces
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]