Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"""': 0.07; 'attribute': 0.07; 'dict': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; '58,': 0.16; '__init__': 0.16; 'invoking': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'subject:problem': 0.19; '(most': 0.21; 'from:addr:web.de': 0.23; 'thanks.': 0.24; 'bruce': 0.24; 'traceback': 0.24; 'classes': 0.26; 'import': 0.27; 'looks': 0.27; 'skip:" 30': 0.28; 'class': 0.29; 'print': 0.29; '(and': 0.30; 'collections': 0.30; 'fails.': 0.30; 'anyone': 0.32; 'object': 0.33; 'file': 0.34; 'header:X-Complaints-To:1': 0.34; '17,': 0.34; 'last):': 0.34; 'root': 0.34; 'to:addr:python-list': 0.35; 'skip:" 20': 0.35; 'received:org': 0.36; 'skip:_ 10': 0.38; 'skip:o 20': 0.38; 'subject:from': 0.39; 'except': 0.39; 'to:addr:python.org': 0.40; 'stuff:': 0.84; 'works)': 0.84; 'before?': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Inheriting from OrderedDict causes problem Followup-To: gmane.comp.python.general Date: Wed, 22 Feb 2012 18:10:25 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849e28.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1329930613 news.xs4all.nl 6962 [2001:888:2000:d::a6]:38274 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.stben.net!goblin1!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:20684 Bruce Eckel wrote: > Notice that both classes are identical, except that one inherits from > dict (and works) and the other inherits from OrderedDict and fails. > Has anyone seen this before? Thanks. > > import collections > > class Y(dict): > def __init__(self, stuff): > for k, v in stuff: > self[k] = v > > # This works: > print Y([('a', 'b'), ('c', 'd')]) > > class X(collections.OrderedDict): > def __init__(self, stuff): > for k, v in stuff: > self[k] = v > > # This doesn't: > print X([('a', 'b'), ('c', 'd')]) > > """ Output: > {'a': 'b', 'c': 'd'} > Traceback (most recent call last): > File "OrderedDictInheritance.py", line 17, in > print X([('a', 'b'), ('c', 'd')]) > File "OrderedDictInheritance.py", line 14, in __init__ > self[k] = v > File "C:\Python27\lib\collections.py", line 58, in __setitem__ > root = self.__root > AttributeError: 'X' object has no attribute '_OrderedDict__root' > """ Looks like invoking OrderedDict.__init__() is necessary: >>> from collections import OrderedDict >>> class X(OrderedDict): ... def __init__(self, stuff): ... super(X, self).__init__() ... for k, v in stuff: ... self[k] = v ... >>> X([("a", "b"), ("c", "d")]) X([('a', 'b'), ('c', 'd')])