]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] Programming FAQ: Mention object.__setattr__ as a technique for delegation...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Thu, 26 Sep 2024 19:19:57 +0000 (21:19 +0200)
committerGitHub <noreply@github.com>
Thu, 26 Sep 2024 19:19:57 +0000 (12:19 -0700)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Doc/faq/programming.rst

index 55432ab79d58d3611f4b73b435ad7f8674d2dcb8..7a86701116413bc6889f7e925a1005bf2ac2c978 100644 (file)
@@ -1613,9 +1613,16 @@ 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__ <object.__dict__>` to store
-local state for self without causing an infinite recursion.
+Many :meth:`~object.__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 set attributes by inserting
+entries into :attr:`self.__dict__ <object.__dict__>` directly.
 
 
 How do I call a method defined in a base class from a derived class that extends it?