replacing __dict__ with an OrderedDict
alex23
wuwei23 at gmail.com
Sun Jan 8 23:58:25 EST 2012
On Jan 7, 2:06 am, Ian Kelly <ian.g.ke... at gmail.com> wrote:
> <ulrich.eckha... at dominolaser.com> wrote:
> > Nonetheless, I'm still wondering if I could somehow replace the dict with an
> > OrderedDict.
>
> In Python 3, yes. This is pretty much the entire use case for the new
> __prepare__ method of metaclasses. See the "OrderedClass" example[...]
This isn't accurate. The OrderedClass example uses an OrderedDict to
remember the method creation order:
def __new__(cls, name, bases, classdict):
result = type.__new__(cls, name, bases, dict(classdict))
result.members = tuple(classdict)
return result
The instantiated objects __dict__ will still be a regularly
dictionary, while the assignment order is stored in the class
attribute .members.
More information about the Python-list
mailing list