basic inheritance question

Nathan Valentine nathan at uky.edu
Thu Sep 20 10:21:38 EDT 2001


>From the Python Library Reference regarding UserDict:

...addition to supporting the methods and operations of mappings (see
section 2.1.6)...

One of the methods of the mapping types is .keys(). .keys() returns
a list of keys in the dictionary.

BTW, please excuse the indentation of the example code. I have feeling
that in pasting the code into this message the indentation may be off.
Looks fine for me, but...

#
# MyDict.py - extend UserDict=20
#

import sys
from UserDict import UserDict

class MyDict(UserDict):
    "extend UserDict"
     def __init__(self):
          print 'Hello from MyDict.__init__'>>> import MyDict

>>> dir(MyDict)
['MyDict', 'UserDict', '__builtins__', '__doc__', '__file__',
'__name__', '=
sys']
>>> dir(MyDict.MyDict)
['__cmp__', '__delitem__', '__doc__', '__getitem__', '__init__',
'__len__',=
 '__module__', '__repr__', '__setitem__', 'clear', 'copy', 'get',
'has_key'=
, 'items', 'keys', 'update', 'values']
>>> foobar =3D MyDict.MyDict()
Hello from MyDict.__init__
>>> foobar.keys
<method UserDict.keys of MyDict instance at 8068b90>
>>> foobar.keys()
Traceback (innermost last):
  File "<stdin>", line 1, in ?
  File "/usr/lib/python1.5/UserDict.py", line 23, in keys
    def keys(self): return self.data.keys()
AttributeError: data


Can someone explain why I am not able to call the .keys() method of
the MyDict even though it descends from UserDict?

Thanks.



More information about the Python-list mailing list