From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Thu, 26 Sep 2024 20:55:08 +0000 (+0200) Subject: [3.13] Programming FAQ: Mention object.__setattr__ as a technique for delegation... X-Git-Tag: v3.13.0rc3~70 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ce63ea0fe42b8934af7aeb69979f1cd2d526b8c6;p=thirdparty%2FPython%2Fcpython.git [3.13] Programming FAQ: Mention object.__setattr__ as a technique for delegation (GH-124617) (#124624) Programming FAQ: Mention object.__setattr__ as a technique for delegation (GH-124617) This is used for example by threading.local in the stdlib. (cherry picked from commit 43979fad904bcc343f90cb526faa526c45fcbfa4) Co-authored-by: Jelle Zijlstra --- diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index 4a6f1ca57d89..fa7b22bde1dc 100644 --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -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__ ` 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__ ` directly. How do I call a method defined in a base class from a derived class that extends it?