@@ -1155,9 +1155,6 @@ \subsection{Customizing attribute access\label{attribute-access}}
11551155The following methods can be defined to customize the meaning of
11561156attribute access (use of, assignment to, or deletion of \code {x.name})
11571157for class instances.
1158- For performance reasons, these methods are cached in the class object
1159- at class definition time; therefore, they cannot be changed after the
1160- class definition is executed.
11611158
11621159\begin {methoddesc }[object]{__getattr__}{self, name}
11631160Called when an attribute lookup has not found the attribute in the
@@ -1171,10 +1168,11 @@ \subsection{Customizing attribute access\label{attribute-access}}
11711168asymmetry between \method {__getattr__()} and \method {__setattr__()}.)
11721169This is done both for efficiency reasons and because otherwise
11731170\method {__setattr__()} would have no way to access other attributes of
1174- the instance.
1175- Note that at least for instance variables, you can fake
1176- total control by not inserting any values in the instance
1177- attribute dictionary (but instead inserting them in another object).
1171+ the instance. Note that at least for instance variables, you can fake
1172+ total control by not inserting any values in the instance attribute
1173+ dictionary (but instead inserting them in another object). See the
1174+ \method {__getattribute__()} method below for a way to actually get
1175+ total control in new-style classes.
11781176\withsubitem {(object method)}{\ttindex {__setattr__()}}
11791177\end {methoddesc }
11801178
@@ -1188,7 +1186,10 @@ \subsection{Customizing attribute access\label{attribute-access}}
11881186should not simply execute \samp {self.\var {name} = value} --- this
11891187would cause a recursive call to itself. Instead, it should insert the
11901188value in the dictionary of instance attributes, e.g.,
1191- \samp {self.__dict__[\var {name}] = value}.
1189+ \samp {self.__dict__[\var {name}] = value}. For new-style classes,
1190+ rather than accessing the instance dictionary, it should call the base
1191+ class method with the same name, for example,
1192+ \samp {object.__setattr__(self, name, value)}.
11921193\withsubitem {(instance attribute)}{\ttindex {__dict__}}
11931194\end {methoddesc }
11941195
@@ -1198,6 +1199,51 @@ \subsection{Customizing attribute access\label{attribute-access}}
11981199obj.\var {name}} is meaningful for the object.
11991200\end {methoddesc }
12001201
1202+ \subsubsection {More attribute access for new-style classes \lable {new-style-attribute-access} }
1203+
1204+ The following methods only apply to new-style classes.
1205+
1206+ \begin {methoddesc }[object]{__getattribute__}{self, name}
1207+ Called unconditionally to implement attribute accesses for instances
1208+ of the class. If the class also defines \method {__getattr__}, it will
1209+ never be called (unless called explicitly).
1210+ This method should return the (computed) attribute
1211+ value or raise an \exception {AttributeError} exception.
1212+ In order to avoid infinite recursion in this method, its
1213+ implementation should always call the base class method with the same
1214+ name to access any attributes it needs to access, for example,
1215+ \samp {object.__getattribute__(self, name)}.
1216+ \end {methoddesc }
1217+
1218+ \subsubsubsection {Implementing Descriptors \label {descriptors }}
1219+
1220+ The following methods only apply when an instance of the class
1221+ containing the method (a so-called \emph {descriptor } class) is in
1222+ the class dictionary of another new-style class, known as the
1223+ \emph {owner } class. In the examples below, `` the attribute'' refers to
1224+ the attribute whose name is the key of the property in the accessed
1225+ class' \code {__dict__}.
1226+
1227+ \begin {methoddesc }[object]{__get__}{self, instance, owner}
1228+ Called to get the attribute of the owner class (class attribute acess)
1229+ or of an instance of that class (instance attribute acces).
1230+ \var {owner} is always the owner class, while \var {instance} is the
1231+ instance that the attribute was accessed through, or \code {None} when
1232+ the attribute is accessed through the \var {owner}. This method should
1233+ return the (computed) attribute value or raise an
1234+ \exception {AttributeError} exception.
1235+ \end {methoddesc }
1236+
1237+ \begin {methoddesc }[object]{__set__}{self, instance, value}
1238+ Called to set the attribute on an instance \{ instance} of the owner
1239+ class to a new value, \var {value}.
1240+ \end {methoddesc }
1241+
1242+ \begin {methoddesc }[object]{__delete__}{self, instance}
1243+ Called to delete the attribute on an instance \{ instance} of the owner
1244+ class.
1245+ \end {methoddesc }
1246+
12011247
12021248\subsection {Emulating callable objects\label {callable-types } }
12031249
0 commit comments