If I create such an instance of a class
class MyClass(object): pass x = MyClass()
Then I can set the attributes to x as follows:
x.myAttr = 1
or x.__setattr__('myAttr', 1)
However, if I do 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 does not have attribute 'myAttr' 'The same thing happens if I try
d.myAttr = 1
I noticed that in the case of x.myAttr = 1, what actually happens is that
x.__dict__
updated with a new key, so it should be that
d.__setattr__
doesn't work because d doesn't have
d.__dict__
simply because d is a dict. I would really appreciate it if someone could explain in detail what is happening here.
Do all python objects have .__dict__
?
Why d.__setattr__
my attempt to call d.__setattr__
result in an error saying that the attribute does not exist?
Is there a specific hierarchy of built-in types that I should be aware of? A simple link would be greatly appreciated.
python 2.6.4
Windows XP Pro x64 SP2
source share