[Tutor] namespace confusion

Christopher Spears cspears2002 at yahoo.com
Wed Jan 18 23:25:20 CET 2006


Let's say I have two classes:

>>> class super:
...     def hello(self):
...         self.data1 = 'spam'
...
>>> class sub(super):
...     def hola(self):
...         self.data2 = 'eggs'
...

Now let's look in the classes' namespaces using
__dict__:

>>> sub.__dict__
{'__module__': '__main__', '__doc__': None, 'hola':
<function hola at 0x403954fc>}

>>> super.__dict__
{'__module__': '__main__', 'hello': <function hello at
0x4039548c>, '__doc__': None}

I was first confused why 'hello' did not appear in sub
as well.  Then I read about dir():

>>> dir(sub)
['__doc__', '__module__', 'hello', 'hola']

>>> dir(super)
['__doc__', '__module__', 'hello']

The above result makes more sense to me.  Why doesn't
__dict__ give the same response as dir()?  How come I
don't see super in sub's namespace?

-Chris


More information about the Tutor mailing list