[Python-ideas] Ordered storage of keyword arguments
mal at egenix.com
Thu Oct 28 11:27:27 CEST 2010
Chris Rebert wrote:
> On Thu, Oct 28, 2010 at 1:13 AM, M.-A. Lemburg <mal at egenix.com> wrote:
>> I don't see much use in having the order of providing the
>> keyword arguments in a function call always available.
>> Perhaps there's a way to have this optionally, i.e. by
>> allowing odicts to be passed in as keyword argument dict ?!
>> Where I do see a real use is making the order of class
>> attribute and method definition accessible in Python
>> (without having to use meta-class hacks like e.g. Django's
>> ORM does).
> So, you want to make class bodies use (C-implemented) OrderedDicts by
> default, thus rendering metaclass __prepare__() definitions for that
> purpose ("meta-class hacks") superfluous?
The http://www.python.org/dev/peps/pep-3115/ has an interesting
Another good suggestion was to simply use an ordered dict for all
classes, and skip the whole 'custom dict' mechanism. This was based
on the observation that most use cases for a custom dict were for
the purposes of preserving order information. However, this idea has
several drawbacks, first because it means that an ordered dict
implementation would have to be added to the set of built-in types
in Python, and second because it would impose a slight speed (and
complexity) penalty on all class declarations. Later, several people
came up with ideas for use cases for custom dictionaries other
than preserving field orderings, so this idea was dropped.
An ordered dict in C could be optimized to keep the same
performance as the regular dict by only storing the insertion
index together with the dict item and not maintaining these
in a separate list.
Access to the order would be slower, but
it would make its use in timing critical parts of CPython
a lot more attractive. An ordered dict would then require more
memory, but not necessarily introduce a performance hit.
The quoted paragraph starts with "observation that most use
cases for a custom dict were for the purposes of preserving
order information" and ends with "several people came up
with ideas for use cases for custom dictionaries other
than preserving field orderings".
I'd call that a standard case of over-generalization - a
rather popular syndrom in Python-land :-) - but that left aside:
the first part is very true. Python has always tried
to make the most common use case simple, so asking programmers to
use a meta-class to be able to access the order of definitions
in a class definition isn't exactly what the normal Python
programmer would expect.
Named tuples and similar sequence/mapping hybrids could probably
also benefit from having the order of definition readily
available, either directly via an odict cls.__dict__ or
via a new attribute cls.__deforder__ which provides the order
information in form of a tuple of cls.__dict__ keys.
Professional Python Services directly from the Source (#1, Oct 28 2010)
>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
::: Try our new mxODBC.Connect Python Database Interface for free ! ::::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
More information about the Python-ideas