Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.python

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 02-22-2012, 03:53 PM
Bruce Eckel
Guest
 
Posts: n/a
Default Inheriting from OrderedDict causes problem

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 <module>
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'
"""
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 02-22-2012, 04:10 PM
Peter Otten
Guest
 
Posts: n/a
Default Re: Inheriting from OrderedDict causes problem

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 <module>
> 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')])


Reply With Quote
  #3 (permalink)  
Old 02-22-2012, 04:33 PM
Bruce Eckel
Guest
 
Posts: n/a
Default Re: Inheriting from OrderedDict causes problem

On Feb 22, 10:10*am, Peter Otten <__pete...@web.de> wrote:
> 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')])


Thank you! That worked. Languages. Some of them automatically call the
default base-class constructors, others don't. Apparently.
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 06:17 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.