Skip to content

Commit ad9e705

Browse files
committed
Programming FAQ: Mention object.__setattr__ as a technique for delegation
This is used for example by threading.local in the stdlib.
1 parent 986a4e1 commit ad9e705

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Doc/faq/programming.rst

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,9 +1613,15 @@ method too, and it must do so carefully. The basic implementation of
16131613
self.__dict__[name] = value
16141614
...
16151615

1616-
Most :meth:`!__setattr__` implementations must modify
1617-
:attr:`self.__dict__ <object.__dict__>` to store
1618-
local state for self without causing an infinite recursion.
1616+
Many :meth:`!__setattr__` implementations call :meth:`object.__setattr__` to set
1617+
an attribute on self without causing infinite recursion::
1618+
1619+
class X:
1620+
def __setattr__(self, name, value):
1621+
# Custom logic here...
1622+
object.__setattr__(self, name, value)
1623+
1624+
Alternatively, it is possible to modify :attr:`self.__dict__ <object.__dict__>` directly.
16191625

16201626

16211627
How do I call a method defined in a base class from a derived class that extends it?

0 commit comments

Comments
 (0)