Name
__setattr__
Synopsis
__setattr__(self,name,value)
At every request to bind attribute
x.y
(typically, an assignment statement
x.y
=
value),
Python calls x
.__setattr__('y',value
).
Python always calls __setattr__ for any
attribute binding on x; a major difference
from __getattr__ (__setattr__ is closer to new-style classes’
__getattribute__ in this sense). To avoid
recursion, when x
.__setattr__ binds x’s
attributes, it must modify x
.__dict__ directly (e.g., by
x
.__dict__[
name
]=
value),
or better, for a new-style class, delegate (e.g., call
super(C, x).__setattr__('y',value
)).
Python ignores the return value of __setattr__.
If __setattr__ is absent, Python usually
translates
x.y
=
z
into x
.__dict__['y']=
z.