@@ -281,7 +281,9 @@ The new class now logs access to both *name* and *age*:
281
281
INFO:root:Updating 'name' to 'Catherine C'
282
282
INFO:root:Updating 'age' to 20
283
283
284
- The two *Person * instances contain only the private names::
284
+ The two *Person * instances contain only the private names:
285
+
286
+ .. doctest ::
285
287
286
288
>>> vars (pete)
287
289
{'_name': 'Peter P', '_age': 10}
@@ -710,6 +712,38 @@ perform attribute lookup by way of a helper function:
710
712
raise
711
713
return type(obj).__getattr__(obj, name) # __getattr__
712
714
715
+ .. doctest ::
716
+ :hide:
717
+
718
+
719
+ >>> class ClassWithGetAttr :
720
+ ... x = 123
721
+ ... def __getattr__ (self , attr ):
722
+ ... return attr.upper()
723
+ ...
724
+ >>> cw = ClassWithGetAttr()
725
+ >>> cw.y = 456
726
+ >>> getattr_hook(cw, ' x' )
727
+ 123
728
+ >>> getattr_hook(cw, ' y' )
729
+ 456
730
+ >>> getattr_hook(cw, ' z' )
731
+ 'Z'
732
+
733
+ >>> class ClassWithoutGetAttr :
734
+ ... x = 123
735
+ ...
736
+ >>> cwo = ClassWithoutGetAttr()
737
+ >>> cwo.y = 456
738
+ >>> getattr_hook(cwo, ' x' )
739
+ 123
740
+ >>> getattr_hook(cwo, ' y' )
741
+ 456
742
+ >>> getattr_hook(cwo, ' z' )
743
+ Traceback (most recent call last):
744
+ ...
745
+ AttributeError: 'ClassWithoutGetAttr' object has no attribute 'z'
746
+
713
747
So if :meth: `__getattr__ ` exists, it is called whenever :meth: `__getattribute__ `
714
748
raises :exc: `AttributeError ` (either directly or in one of the descriptor calls).
715
749
@@ -1139,8 +1173,8 @@ If you have ever wondered where *self* comes from in regular methods or where
1139
1173
*cls * comes from in class methods, this is it!
1140
1174
1141
1175
1142
- Other kinds of methods
1143
- ----------------------
1176
+ Kinds of methods
1177
+ ----------------
1144
1178
1145
1179
Non-data descriptors provide a simple mechanism for variations on the usual
1146
1180
patterns of binding functions into methods.
@@ -1193,19 +1227,19 @@ example calls are unexciting:
1193
1227
class E:
1194
1228
@staticmethod
1195
1229
def f(x):
1196
- print(x)
1230
+ return x * 10
1197
1231
1198
1232
.. doctest ::
1199
1233
1200
1234
>>> E.f(3 )
1201
- 3
1235
+ 30
1202
1236
>>> E().f(3 )
1203
- 3
1237
+ 30
1204
1238
1205
1239
Using the non-data descriptor protocol, a pure Python version of
1206
1240
:func: `staticmethod ` would look like this:
1207
1241
1208
- .. doctest ::
1242
+ .. testcode ::
1209
1243
1210
1244
class StaticMethod:
1211
1245
"Emulate PyStaticMethod_Type() in Objects/funcobject.c"
@@ -1216,6 +1250,22 @@ Using the non-data descriptor protocol, a pure Python version of
1216
1250
def __get__(self, obj, objtype=None):
1217
1251
return self.f
1218
1252
1253
+ .. testcode ::
1254
+ :hide:
1255
+
1256
+ class E_sim:
1257
+ @StaticMethod
1258
+ def f(x):
1259
+ return x * 10
1260
+
1261
+ .. doctest ::
1262
+ :hide:
1263
+
1264
+ >>> E_sim.f(3 )
1265
+ 30
1266
+ >>> E_sim().f(3 )
1267
+ 30
1268
+
1219
1269
1220
1270
Class methods
1221
1271
-------------
0 commit comments