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__

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!feed.xsnews.nl!border-2.ams.xsnews.nl!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'subject:using': 0.04; 'subject:Python': 0.05; 'initialize': 0.07; 'dict': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'subclass': 0.09; 'intermediate': 0.15; "type's": 0.16; 'instance': 0.18; "aren't": 0.21; 'header:In-Reply-To:1': 0.22; 'wrong?': 0.23; 'code': 0.25; 'header:User-Agent:1': 0.33; 'header:X-Complaints- To:1': 0.33; 'to:addr:python-list': 0.34; 'anything': 0.34; 'something': 0.35; 'url:python': 0.36; 'but': 0.37; 'doing': 0.38; 'steven': 0.38; 'received:org': 0.38; 'allows': 0.38; 'url:docs': 0.39; 'received:de': 0.39; 'url:org': 0.39; "it's": 0.40; 'to:addr:python.org': 0.40; 'order': 0.62; 'skip:o 30': 0.63; 'deliberate': 0.84; 'dict,': 0.84; 'hook': 0.84; 'url:datamodel': 0.84; 'url:reference': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Christian Heimes <lists@cheimes.de>
Subject Re: Using an OrderedDict for __dict__ in Python 3 using __prepare__
Date Mon, 09 Jan 2012 03:46:17 +0100
References <4f0a4f2a$0$11120$c3e8da3@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host f049182114.adsl.alicedsl.de
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:9.0) Gecko/20111229 Thunderbird/9.0
In-Reply-To <4f0a4f2a$0$11120$c3e8da3@news.astraweb.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4539.1326077197.27778.python-list@python.org> (permalink)
Lines 23
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1326077197 news.xs4all.nl 6937 [2001:888:2000:d::a6]:43099
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:18695

Show key headers only | 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