Skip to content

Commit 43979fa

Browse files
authoredSep 26, 2024
Programming FAQ: Mention object.__setattr__ as a technique for delegation (python#124617)
This is used for example by threading.local in the stdlib.
1 parent 986a4e1 commit 43979fa

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed
 

‎Doc/faq/programming.rst

+10-3
Original file line numberDiff line numberDiff line change
@@ -1613,9 +1613,16 @@ 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:`~object.__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 set attributes by inserting
1625+
entries into :attr:`self.__dict__ <object.__dict__>` directly.
16191626

16201627

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

0 commit comments

Comments
 (0)
Please sign in to comment.