In the below example, the superclass has a __dict__
attribute, while the subclass does not have it.
>>> class Super(object):
... def hello(self):
... self.data1="hello"
...
>>>
>>> class Sub(Super):
... def hola(self):
... self.data2="hola"
...
>>>
>>> Super.__dict__
<dictproxy object at 0x108794868>
>>> Super.__dict__.keys()
['__dict__', '__module__', '__weakref__', 'hello', '__doc__'] # note __dict__
>>> Sub.__dict__.keys()
['__module__', '__doc__', 'hola'] #__dict__ absent here
>>> Sub.__dict__
<dictproxy object at 0x108794868>
Q1: The comments on the above shows where dict is present. why the superclass has it but not the sublcass.
while trying to find out the answer for this, I came across this post. and this confused me further.
>>> class Foo(object):
... __slots__ = ('bar',)
... bar="spam"
...
>>> f = Foo()
>>> f.__dict__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Foo' object has no attribute '__dict__'
>>> class A(object):
... pass
...
>>> b = A()
>>> b.__dict__
{}
Q2: why the instance
of Foo
throws AttributeError
but that of A
has empty dict.