Python interaction between __setattr__ and self.__dict__ -
if make class instance this
class myclass(object): pass x = myclass() i can set attributes on x follows:
x.myattr = 1 or x.__setattr__('myattr', 1)
however, if make dict
d = {} i cannot set attributes on it. example, if this
d.__setattr__('myattr', 1) i error " 'dict' object has no attribute 'myattr' " same thing happens if try
d.myattr = 1 i have noticed in case of x.myattr = 1 happens that
x.__dict__ gets updated new key, must that
d.__setattr__ doesn't work because d doesn't have
d.__dict__ simply because d is dict. appreciate if thoroughly explain what's going on here.
do python objects have .__dict__?
why attempt @ calling d.__setattr__ result in error saying attribute doesn't exist?
is there specific heirarchy of built-in types should know about? simple reference appreciated.
python 2.6.4
windows xp pro x64 sp2
a dict instance doesn't have __dict__ attribute, can't assign attributes on it. of python's built-in classes (which written in c , define attributes differently) don't. if subclass dict, subclass have __dict__ attribute , can add attributes instances of subclass.
Comments
Post a Comment