mirror of
https://github.com/python/cpython.git
synced 2026-01-04 14:32:21 +00:00
[3.12] Programming FAQ: Mention object.__setattr__ as a technique for delegation (GH-124617) (#124625)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
parent
a0c90b36f6
commit
27890edd5a
1 changed files with 10 additions and 3 deletions
|
|
@ -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?
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue