[Python-ideas] OrderedDict for kwargs and class statement namespace

Eric Snow ericsnowcurrently at gmail.com
Thu Feb 28 22:37:45 CET 2013


On Thu, Feb 28, 2013 at 11:02 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> And PEP 422 is designed to make it easier to share a common __prepare__
> method with different post processing.

A major use case for __prepare__() is to have the class definition use
an OrderedDict.  It's even a point of discussion in PEP 3115.  While I
agree with the the conclusion of the PEP that using only OrderedDict
is inferior to __prepare__(), I also think defaulting to OrderedDict
is viable and useful.

Using __prepare__() necessitates the use of a metaclass, which most
people consider black magic.  Even when you can inherit from a class
that has a custom metaclass (like collections.abc.ABC), it still
necessitates inheritance and the chance for metaclass conflicts. While
I'm on board with PEP 422, I'm not clear on how it helps here.

If class namespaces were ordered by default then class decorators,
which are typically much easier to comprehend, would steal yet another
use case from metaclasses.  The recent discussions on enums got me
thinking about this.  In some cases you want you class customization
logic to be inherited.  That's where you've got to use a metaclass.
In other cases you don't.  Decorators work great for that.  If your
class decorator needs to have ordering information, then you also have
to use a metaclass.  Having OrderedDict be the default class namespace
would address that.

-eric



More information about the Python-ideas mailing list