From ad9e705fd4df1bf4a46d5025bab10c8e1b8e85ae Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 26 Sep 2024 10:53:38 -0700 Subject: [PATCH 1/2] Programming FAQ: Mention object.__setattr__ as a technique for delegation This is used for example by threading.local in the stdlib. --- Doc/faq/programming.rst | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index 4a6f1ca57d89e3..34478fde1b4588 100644 --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -1613,9 +1613,15 @@ method too, and it must do so carefully. The basic implementation of self.__dict__[name] = value ... -Most :meth:`!__setattr__` implementations must modify -:attr:`self.__dict__ ` to store -local state for self without causing an infinite recursion. +Many :meth:`!__setattr__` implementations call :meth:`object.__setattr__` to set +an attribute on self without causing infinite recursion:: + + class X: + def __setattr__(self, name, value): + # Custom logic here... + object.__setattr__(self, name, value) + +Alternatively, it is possible to modify :attr:`self.__dict__ ` directly. How do I call a method defined in a base class from a derived class that extends it? From 9186b2e7e28dca9444c7f07a40f95d3314972490 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Thu, 26 Sep 2024 11:28:30 -0700 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Alex Waygood --- Doc/faq/programming.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index 34478fde1b4588..fa7b22bde1dc6f 100644 --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -1613,7 +1613,7 @@ method too, and it must do so carefully. The basic implementation of self.__dict__[name] = value ... -Many :meth:`!__setattr__` implementations call :meth:`object.__setattr__` to set +Many :meth:`~object.__setattr__` implementations call :meth:`!object.__setattr__` to set an attribute on self without causing infinite recursion:: class X: @@ -1621,7 +1621,8 @@ an attribute on self without causing infinite recursion:: # Custom logic here... object.__setattr__(self, name, value) -Alternatively, it is possible to modify :attr:`self.__dict__ ` directly. +Alternatively, it is possible to set attributes by inserting +entries into :attr:`self.__dict__ ` directly. How do I call a method defined in a base class from a derived class that extends it?