Careful. The two msg attributes are actually stored in two different dictionaries.
One overshadows the other, but the clobbered msg
attribute is still taking up space in a dictionary. So it goes unused and yet still takes up some memory.
class MyClass(object):
msg = 'FeeFiFoFum'
def __init__(self, msg):
self.msg = msg
m=MyClass('Hi Lucy')
Notice that we have 'Hi Lucy'
as the value.
print(m.__dict__)
# {'msg': 'Hi Lucy'}
Notice that MyClass's dict (accessed through m.__class__
) still has FeeFiFoFum
.
print(m.__class__.__dict__)
# {'__dict__': <attribute '__dict__' of 'MyClass' objects>, '__module__': '__main__', '__init__': <function __init__ at 0xb76ea1ec>, 'msg': 'FeeFiFoFum', 'some_dict': {}, '__weakref__': <attribute '__weakref__' of 'MyClass' objects>, '__doc__': 'my docstring', 'a_variable': None}
Another (perhaps simpler) way to see this:
print(m.msg)
# Hi Lucy
print(MyClass.msg)
# FeeFiFoFum