If I create a class A as follows:
class A: def __init__(self): self.name = 'A'
Inspecting the __dict__ member looks like {'name': 'A'}
If however I create a class B:
class B: name = 'B'
__dict__ is empty.
What is the difference between the two, and why doesn’t name show up in B‘s __dict__?
B.nameis a class attribute, not an instance attribute. It shows up inB.__dict__, but not inb = B(); b.__dict__.The distinction is obscured somewhat because when you access an attribute on an instance, the class dict is a fallback. So in the above example,
b.namewill give you the value ofB.name.