@@ -42,7 +42,7 @@ add new capabilities one by one.
42
42
Simple example: A descriptor that returns a constant
43
43
----------------------------------------------------
44
44
45
- The :class: `Ten ` class is a descriptor whose :meth: `__get__ ` method always
45
+ The :class: `! Ten ` class is a descriptor whose :meth: `~object. __get__ ` method always
46
46
returns the constant ``10 ``:
47
47
48
48
.. testcode ::
@@ -120,10 +120,10 @@ different, updated answers each time::
120
120
2
121
121
122
122
Besides showing how descriptors can run computations, this example also
123
- reveals the purpose of the parameters to :meth: `__get__ `. The *self *
123
+ reveals the purpose of the parameters to :meth: `~object. __get__ `. The *self *
124
124
parameter is *size *, an instance of *DirectorySize *. The *obj * parameter is
125
125
either *g * or *s *, an instance of *Directory *. It is the *obj * parameter that
126
- lets the :meth: `__get__ ` method learn the target directory. The *objtype *
126
+ lets the :meth: `~object. __get__ ` method learn the target directory. The *objtype *
127
127
parameter is the class *Directory *.
128
128
129
129
@@ -133,7 +133,7 @@ Managed attributes
133
133
A popular use for descriptors is managing access to instance data. The
134
134
descriptor is assigned to a public attribute in the class dictionary while the
135
135
actual data is stored as a private attribute in the instance dictionary. The
136
- descriptor's :meth: `__get__ ` and :meth: `__set__ ` methods are triggered when
136
+ descriptor's :meth: `~object. __get__ ` and :meth: `~object. __set__ ` methods are triggered when
137
137
the public attribute is accessed.
138
138
139
139
In the following example, *age * is the public attribute and *_age * is the
@@ -215,9 +215,9 @@ Customized names
215
215
When a class uses descriptors, it can inform each descriptor about which
216
216
variable name was used.
217
217
218
- In this example, the :class: `Person ` class has two descriptor instances,
219
- *name * and *age *. When the :class: `Person ` class is defined, it makes a
220
- callback to :meth: `__set_name__ ` in *LoggedAccess * so that the field names can
218
+ In this example, the :class: `! Person ` class has two descriptor instances,
219
+ *name * and *age *. When the :class: `! Person ` class is defined, it makes a
220
+ callback to :meth: `~object. __set_name__ ` in *LoggedAccess * so that the field names can
221
221
be recorded, giving each descriptor its own *public_name * and *private_name *:
222
222
223
223
.. testcode ::
@@ -253,8 +253,8 @@ be recorded, giving each descriptor its own *public_name* and *private_name*:
253
253
def birthday(self):
254
254
self.age += 1
255
255
256
- An interactive session shows that the :class: `Person ` class has called
257
- :meth: `__set_name__ ` so that the field names would be recorded. Here
256
+ An interactive session shows that the :class: `! Person ` class has called
257
+ :meth: `~object. __set_name__ ` so that the field names would be recorded. Here
258
258
we call :func: `vars ` to look up the descriptor without triggering it:
259
259
260
260
.. doctest ::
@@ -294,10 +294,10 @@ The two *Person* instances contain only the private names:
294
294
Closing thoughts
295
295
----------------
296
296
297
- A :term: `descriptor ` is what we call any object that defines :meth: `__get__ `,
298
- :meth: `__set__ `, or :meth: `__delete__ `.
297
+ A :term: `descriptor ` is what we call any object that defines :meth: `~object. __get__ `,
298
+ :meth: `~object. __set__ `, or :meth: `~object. __delete__ `.
299
299
300
- Optionally, descriptors can have a :meth: `__set_name__ ` method. This is only
300
+ Optionally, descriptors can have a :meth: `~object. __set_name__ ` method. This is only
301
301
used in cases where a descriptor needs to know either the class where it was
302
302
created or the name of class variable it was assigned to. (This method, if
303
303
present, is called even if the class is not a descriptor.)
@@ -337,7 +337,7 @@ any data, it verifies that the new value meets various type and range
337
337
restrictions. If those restrictions aren't met, it raises an exception to
338
338
prevent data corruption at its source.
339
339
340
- This :class: `Validator ` class is both an :term: `abstract base class ` and a
340
+ This :class: `! Validator ` class is both an :term: `abstract base class ` and a
341
341
managed attribute descriptor:
342
342
343
343
.. testcode ::
@@ -360,22 +360,22 @@ managed attribute descriptor:
360
360
def validate(self, value):
361
361
pass
362
362
363
- Custom validators need to inherit from :class: `Validator ` and must supply a
364
- :meth: `validate ` method to test various restrictions as needed.
363
+ Custom validators need to inherit from :class: `! Validator ` and must supply a
364
+ :meth: `! validate ` method to test various restrictions as needed.
365
365
366
366
367
367
Custom validators
368
368
-----------------
369
369
370
370
Here are three practical data validation utilities:
371
371
372
- 1) :class: `OneOf ` verifies that a value is one of a restricted set of options.
372
+ 1) :class: `! OneOf ` verifies that a value is one of a restricted set of options.
373
373
374
- 2) :class: `Number ` verifies that a value is either an :class: `int ` or
374
+ 2) :class: `! Number ` verifies that a value is either an :class: `int ` or
375
375
:class: `float `. Optionally, it verifies that a value is between a given
376
376
minimum or maximum.
377
377
378
- 3) :class: `String ` verifies that a value is a :class: `str `. Optionally, it
378
+ 3) :class: `! String ` verifies that a value is a :class: `str `. Optionally, it
379
379
validates a given minimum or maximum length. It can validate a
380
380
user-defined `predicate
381
381
<https://en.wikipedia.org/wiki/Predicate_(mathematical_logic)> `_ as well.
@@ -498,8 +498,8 @@ Definition and introduction
498
498
---------------------------
499
499
500
500
In general, a descriptor is an attribute value that has one of the methods in
501
- the descriptor protocol. Those methods are :meth: `__get__ `, :meth: `__set__ `,
502
- and :meth: `__delete__ `. If any of those methods are defined for an
501
+ the descriptor protocol. Those methods are :m
F438
eth: `~object. __get__ `, :meth: `~object. __set__ `,
502
+ and :meth: `~object. __delete__ `. If any of those methods are defined for an
503
503
attribute, it is said to be a :term: `descriptor `.
504
504
505
505
The default behavior for attribute access is to get, set, or delete the
@@ -531,8 +531,8 @@ That is all there is to it. Define any of these methods and an object is
531
531
considered a descriptor and can override default behavior upon being looked up
532
532
as an attribute.
533
533
534
- If an object defines :meth: `__set__ ` or :meth: `__delete__ `, it is considered
535
- a data descriptor. Descriptors that only define :meth: `__get__ ` are called
534
+ If an object defines :meth: `~object. __set__ ` or :meth: `~object. __delete__ `, it is considered
535
+ a data descriptor. Descriptors that only define :meth: `~object. __get__ ` are called
536
536
non-data descriptors (they are often used for methods but other uses are
537
537
possible).
538
538
@@ -542,9 +542,9 @@ has an entry with the same name as a data descriptor, the data descriptor
542
542
takes precedence. If an instance's dictionary has an entry with the same
543
543
name as a non-data descriptor, the dictionary entry takes precedence.
544
544
545
- To make a read-only data descriptor, define both :meth: `__get__ ` and
546
- :meth: `__set__ ` with the :meth: `__set__ ` raising an :exc: `AttributeError ` when
547
- called. Defining the :meth: `__set__ ` method with an exception raising
545
+ To make a read-only data descriptor, define both :meth: `~object. __get__ ` and
546
+ :meth: `~object. __set__ ` with the :meth: `~object. __set__ ` raising an :exc: `AttributeError ` when
547
+ called. Defining the :meth: `~object. __set__ ` method with an exception raising
548
548
placeholder is enough to make it a data descriptor.
549
549
550
550
@@ -571,7 +571,7 @@ Invocation from an instance
571
571
572
572
Instance lookup scans through a chain of namespaces giving data descriptors
573
573
the highest priority, followed by instance variables, then non-data
574
- descriptors, then class variables, and lastly :meth: `__getattr__ ` if it is
574
+ descriptors, then class variables, and lastly :meth: `~object. __getattr__ ` if it is
575
575
provided.
576
576
577
577
If a descriptor is found for ``a.x ``, then it is invoked with:
@@ -716,12 +716,12 @@ a pure Python equivalent:
716
716
>>> object_getattribute(u2, ' x' ) == u2.x == (D1, u2, U2)
717
717
True
718
718
719
- Note, there is no :meth: `__getattr__ ` hook in the :meth: `__getattribute__ `
720
- code. That is why calling :meth: `__getattribute__ ` directly or with
721
- ``super().__getattribute__ `` will bypass :meth: `__getattr__ ` entirely.
719
+ Note, there is no :meth: `~object. __getattr__ ` hook in the :meth: `~object. __getattribute__ `
720
+ code. That is why calling :meth: `~object. __getattribute__ ` directly or with
721
+ ``super().__getattribute__ `` will bypass :meth: `~object. __getattr__ ` entirely.
722
722
723
723
Instead, it is the dot operator and the :func: `getattr ` function that are
724
- responsible for invoking :meth: `__getattr__ ` whenever :meth: `__getattribute__ `
724
+ responsible for invoking :meth: `~object. __getattr__ ` whenever :meth: `~object. __getattribute__ `
725
725
raises an :exc: `AttributeError `. Their logic is encapsulated in a helper
726
726
function:
727
727
@@ -773,8 +773,8 @@ Invocation from a class
773
773
-----------------------
774
774
775
775
The logic for a dotted lookup such as ``A.x `` is in
776
- :meth: `type.__getattribute__ `. The steps are similar to those for
777
- :meth: `object.__getattribute__ ` but the instance dictionary lookup is replaced
776
+ :meth: `! type.__getattribute__ `. The steps are similar to those for
777
+ :meth: `! object.__getattribute__ ` but the instance dictionary lookup is replaced
778
778
by a search through the class's :term: `method resolution order `.
779
779
780
780
If a descriptor is found, it is invoked with ``desc.__get__(None, A) ``.
@@ -786,7 +786,7 @@ The full C implementation can be found in :c:func:`!type_getattro` and
786
786
Invocation from super
787
787
---------------------
788
788
789
- The logic for super's dotted lookup is in the :meth: `__getattribute__ ` method for
789
+ The logic for super's dotted lookup is in the :meth: `~object. __getattribute__ ` method for
790
790
object returned by :func: `super `.
791
791
792
792
A dotted lookup such as ``super(A, obj).m `` searches ``obj.__class__.__mro__ ``
@@ -803,21 +803,21 @@ The full C implementation can be found in :c:func:`!super_getattro` in
803
803
Summary of invocation logic
804
804
---------------------------
805
805
806
- The mechanism for descriptors is embedded in the :meth: `__getattribute__ `
806
+ The mechanism for descriptors is embedded in the :meth: `~object. __getattribute__ `
807
807
methods for :class: `object `, :class: `type `, and :func: `super `.
808
808
809
809
The important points to remember are:
810
810
811
- * Descriptors are invoked by the :meth: `__getattribute__ ` method.
811
+ * Descriptors are invoked by the :meth: `~object. __getattribute__ ` method.
812
812
813
813
* Classes inherit this machinery from :class: `object `, :class: `type `, or
814
814
:func: `super `.
815
815
816
- * Overriding :meth: `__getattribute__ ` prevents automatic descriptor calls
816
+ * Overriding :meth: `~object. __getattribute__ ` prevents automatic descriptor calls
817
817
because all the descriptor logic is in that method.
818
818
819
- * :meth: `object.__getattribute__ ` and :meth: `type.__getattribute__ ` make
820
- different calls to :meth: `__get__ `. The first includes the instance and may
819
+ * :meth: `! object.__getattribute__ ` and :meth: `! type.__getattribute__ ` make
820
+ different calls to :meth: `~object. __get__ `. The first includes the instance and may
821
821
include the class. The second puts in ``None `` for the instance and always
822
822
includes the class.
823
823
@@ -832,16 +832,16 @@ Automatic name notification
832
832
Sometimes it is desirable for a descriptor to know what class variable name it
833
833
was assigned to. When a new class is created, the :class: `type ` metaclass
834
834
scans the dictionary of the new class. If any of the entries are descriptors
835
- and if they define :meth: `__set_name__ `, that method is called with two
835
+ and if they define :meth: `~object. __set_name__ `, that method is called with two
836
836
arguments. The *owner * is the class where the descriptor is used, and the
837
837
*name * is the class variable the descriptor was assigned to.
838
838
839
839
The implementation details are in :c:func: `!type_new ` and
840
840
:c:func: `!set_names ` in :source: `Objects/typeobject.c `.
841
841
842
- Since the update logic is in :meth: `type.__new__ `, notifications only take
842
+ Since the update logic is in :meth: `! type.__new__ `, notifications only take
843
843
place at the time of class creation. If descriptors are added to the class
844
- afterwards, :meth: `__set_name__ ` will need to be called manually.
844
+ afterwards, :meth: `~object. __set_name__ ` will need to be called manually.
845
845
846
846
847
847
ORM example
@@ -870,7 +870,7 @@ care of lookups or updates:
870
870
conn.execute(self.store, [value, obj.key])
871
871
conn.commit()
872
872
873
- We can use the :class: `Field ` class to define `models
873
+ We can use the :class: `! Field ` class to define `models
874
874
<https://en.wikipedia.org/wiki/Database_model> `_ that describe the schema for
875
875
each table in a database:
876
876
@@ -1150,7 +1150,7 @@ to wrap access to the value attribute in a property data descriptor:
1150
1150
self.recalc()
1151
1151
return self._value
1152
1152
1153
- Either the built-in :func: `property ` or our :func: `Property ` equivalent would
1153
+ Either the built-in :func: `property ` or our :func: `! Property ` equivalent would
1154
1154
work in this example.
1155
1155
1156
1156
@@ -1183,7 +1183,7 @@ roughly equivalent to:
1183
1183
return func(obj, *args, **kwargs)
1184
1184
1185
1185
To support automatic creation of methods, functions include the
1186
- :meth: `__get__ ` method for binding methods during attribute access. This
1186
+ :meth: `~object. __get__ ` method for binding methods during attribute access. This
1187
1187
means that functions are non-data descriptors that return bound methods
1188
1188
during dotted lookup from an instance. Here's how it works:
1189
1189
@@ -1215,19 +1215,19 @@ The function has a :term:`qualified name` attribute to support introspection:
1215
1215
'D.f'
1216
1216
1217
1217
Accessing the function through the class dictionary does not invoke
1218
- :meth: `__get__ `. Instead, it just returns the underlying function object::
1218
+ :meth: `~object. __get__ `. Instead, it just returns the underlying function object::
1219
1219
1220
1220
>>> D.__dict__['f']
1221
1221
<function D.f at 0x00C45070>
1222
1222
1223
- Dotted access from a class calls :meth: `__get__ ` which just returns the
1223
+ Dotted access from a class calls :meth: `~object. __get__ ` which just returns the
1224
1224
underlying function unchanged::
1225
1225
1226
1226
>>> D.f
1227
1227
<function D.f at 0x00C45070>
1228
1228
1229
1229
The interesting behavior occurs during dotted access from an instance. The
1230
- dotted lookup calls :meth: `__get__ ` which returns a bound method object::
1230
+ dotted lookup calls :meth: `~object. __get__ ` which returns a bound method object::
1231
1231
1232
1232
>>> d = D()
1233
1233
>>> d.f
@@ -1252,7 +1252,7 @@ Kinds of methods
1252
1252
Non-data descriptors provide a simple mechanism for variations on the usual
1253
1253
patterns of binding functions into methods.
1254
1254
1255
- To recap, functions have a :meth: `__get__ ` method so that they can be converted
1255
+ To recap, functions have a :meth: `~object. __get__ ` method so that they can be converted
1256
1256
to a method when accessed as attributes. The non-data descriptor transforms an
1257
1257
``obj.f(*args) `` call into ``f(obj, *args) ``. Calling ``cls.f(*args) ``
1258
1258
becomes ``f(*args) ``.
@@ -1682,7 +1682,7 @@ by member descriptors:
1682
1682
'Emulate member_repr() in Objects/descrobject.c'
1683
1683
return f'<Member {self.name!r} of {self.clsname!r}>'
1684
1684
1685
- The :meth: `type.__new__ ` method takes care of adding member objects to class
1685
+ The :meth: `! type.__new__ ` method takes care of adding member objects to class
1686
1686
variables:
1687
1687
1688
1688
.. testcode ::
@@ -1733,7 +1733,7 @@ Python:
1733
1733
)
1734
1734
super().__delattr__(name)
1735
1735
1736
- To use the simulation in a real class, just inherit from :class: `Object ` and
1736
+ To use the simulation in a real class, just inherit from :class: `! Object ` and
1737
1737
set the :term: `metaclass ` to :class: `Type `:
1738
1738
1739
1739
.. testcode ::
0 commit comments