Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #18695

Re: Using an OrderedDict for __dict__ in Python 3 using __prepare__

From Christian Heimes <lists@cheimes.de>
Subject Re: Using an OrderedDict for __dict__ in Python 3 using __prepare__
Date 2012-01-09 03:46 +0100
References <4f0a4f2a$0$11120$c3e8da3@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.4539.1326077197.27778.python-list@python.org> (permalink)

Show all headers | View raw


Am 09.01.2012 03:21, schrieb Steven D'Aprano:
> What am I doing wrong?

You aren't doing anything wrong. It's just not possible to have
something different than a dict as a type's __dict__. It's a deliberate
limitation and required optimization. The __prepare__ hook allows to you
have a dict subclass as intermediate dict, but in the end it always
comes down to a dict.

The code in Objects/typeobject.c:type_new() copies the dict:

    /* Initialize tp_dict from passed-in dict */
    type->tp_dict = dict = PyDict_Copy(dict);

PyDict_Copy() takes an instance of a PyDict_Type subclass and always
returns a PyDictObject.

However you can use the __prepare__ hook to *remember* the order of
insertion, see
http://docs.python.org/py3k/reference/datamodel.html#customizing-class-creation

Christian

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Using an OrderedDict for __dict__ in Python 3 using __prepare__ Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-01-09 02:21 +0000
  Re: Using an OrderedDict for __dict__ in Python 3 using __prepare__ Christian Heimes <lists@cheimes.de> - 2012-01-09 03:46 +0100

csiph-web