mirror of https://github.com/python/cpython
Programming FAQ: Mention object.__setattr__ as a technique for delegation (#124617)
This is used for example by threading.local in the stdlib.
This commit is contained in:
parent
986a4e1b6f
commit
43979fad90
|
@ -1613,9 +1613,16 @@ method too, and it must do so carefully. The basic implementation of
|
||||||
self.__dict__[name] = value
|
self.__dict__[name] = value
|
||||||
...
|
...
|
||||||
|
|
||||||
Most :meth:`!__setattr__` implementations must modify
|
Many :meth:`~object.__setattr__` implementations call :meth:`!object.__setattr__` to set
|
||||||
:attr:`self.__dict__ <object.__dict__>` to store
|
an attribute on self without causing infinite recursion::
|
||||||
local state for self without causing an 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?
|
How do I call a method defined in a base class from a derived class that extends it?
|
||||||
|
|
Loading…
Reference in New Issue