[Tutor] namespace confusion

bob bgailer at alum.rpi.edu
Thu Jan 19 00:46:10 CET 2006


At 02:25 PM 1/18/2006, Christopher Spears wrote:
>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?

 From help under dir():
"With an argument, attempts to return a list of valid attributes for 
that object. This information is gleaned from the object's __dict__ 
attribute, if defined, and from the class or type object. The list is 
not necessarily complete. ... If the object is a type or class 
object, the list contains the names of its attributes, and 
recursively of the attributes of its bases.: 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060118/e633bd6d/attachment.html 


More information about the Tutor mailing list