[Python-ideas] Dict literal use for custom dict classes

Eric Snow ericsnowcurrently at gmail.com
Thu Dec 17 13:37:09 EST 2015


On Wed, Dec 16, 2015 at 2:50 AM, M.-A. Lemburg <mal at egenix.com> wrote:
> Since requests to be able to access the order of values,
> parameters and definitions in source code come up rather
> often, perhaps it'd better to provide Python application
> with a standard access mechanism to this order rather than
> trying to push use of OrderedDict and the like into the
> runtime, causing unnecessary performance overhead.
>
> The parser does have access to this information in the AST
> and some of it is partially copied into code object attributes,
> but there's no general purpose access to the information.
>
> Based on the source code order, you could do lots of
> things, e.g. avoid hacks to map class attributes to
> column definitions for ORMs, make it possible to write
> OrderedDict(a=x, b=y) and have the literal order preserved,
> have NamedTuple(a=x, b=y) work without additional tricks,
> etc.
>
> What's important here is that the runtime performance
> would not change. The code objects would
> gain some additional tuples, which store the order of
> the literals used in their AST, so only the memory
> consumption would increase.

+1 from me.  That's essentially the goal of PEP 468. [1]  While the
proposed solution focuses on OrderedDict, note the various
alternatives at the bottom of the PEP.  Also note that OrderedDict now
has a C implementation that doesn't suffer from the same performance
penalty. [2]

-eric

[1] http://legacy.python.org/dev/peps/pep-0468/
[2] OrderedDict is actually faster for iteration, the same speed for
other non-mutation operations, not much slower for most mutation
operations, and 4x slower in the worst case.  That is a drastic
improvement over the pure Python OrderedDict.


More information about the Python-ideas mailing list