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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access