If I make a class instance like this
class MyClass(object):
pass
x = MyClass()
I can then set attributes on x as follows:
x.myAttr = 1 or x.__setattr__('myAttr', 1)
However, if I make a dict
d = {}
I cannot set attributes on it. For example, if I do this
d.__setattr__('myAttr', 1)
I get the error " 'dict' object has no attribute 'myAttr' " The same thing happens if I try
d.myAttr = 1
I have noticed that in the case of x.myAttr = 1 what actually happens is that
x.__dict__
gets updated with a new key, so it must be that
d.__setattr__
doesn't work because d doesn't have
d.__dict__
simply because d is a dict. I would appreciate it very much if someone could thoroughly explain what's going on here.
Do all python objects have .__dict__?
Why does my attempt at calling d.__setattr__ result in an error saying that the attribute doesn't exist?
Is there a specific heirarchy of built-in types that I should know about? A simple reference would be much appreciated.
python 2.6.4
Windows XP Pro x64 SP2