@@ -27,8 +27,9 @@ This guide has four major sections:
27
27
28
28
4) The last section has pure Python equivalents for built-in descriptors that
29
29
are written in C. Read this if you're curious about how functions turn
30
- into bound methods or about how to implement common tools like
31
- :func: `classmethod `, :func: `staticmethod `, and :func: `property `.
30
+ into bound methods or about the implementation of common tools like
31
+ :func: `classmethod `, :func: `staticmethod `, :func: `property `, and
32
+ :term: `__slots__ `.
32
33
33
34
34
35
Primer
@@ -188,7 +189,7 @@ logged attribute and that its name is unchangeable. In the next example,
188
189
we'll fix that problem.
189
190
190
191
191
- Customized Names
192
+ Customized names
192
193
----------------
193
194
194
195
When a class uses descriptors, it can inform each descriptor about what
@@ -438,7 +439,7 @@ creates a deeper understanding of how Python works and an appreciation for the
438
439
elegance of its design.
439
440
440
441
441
- Definition and Introduction
442
+ Definition and introduction
442
443
---------------------------
443
444
444
445
In general, a descriptor is an object attribute with "binding behavior", one
@@ -463,7 +464,7 @@ simplify the underlying C code and offer a flexible set of new tools for
463
464
everyday Python programs.
464
465
465
466
466
- Descriptor Protocol
467
+ Descriptor protocol
467
468
-------------------
468
469
469
470
``descr.__get__(self, obj, type=None) -> value ``
@@ -493,7 +494,7 @@ called. Defining the :meth:`__set__` method with an exception raising
493
494
placeholder is enough to make it a data descriptor.
494
495
495
496
496
- Overview of Descriptor Invocation
497
+ Overview of descriptor invocation
497
498
---------------------------------
498
499
499
500
A descriptor can be called directly with ``desc.__get__(obj) `` or
@@ -510,7 +511,7 @@ The details of invocation depend on whether ``obj`` is an object, class, or
510
511
instance of super.
511
512
512
513
513
- Invocation from an Instance
514
+ Invocation from an instance
514
515
---------------------------
515
516
516
517
Instance lookup scans through a chain of namespaces giving data descriptors
@@ -549,7 +550,7 @@ The :exc:`TypeError` exception handler is needed because the instance dictionary
549
550
doesn't exist when its class defines :term: `__slots__ `.
550
551
551
552
552
- Invocation from a Class
553
+ Invocation from a class
553
554
-----------------------
554
555
555
556
The logic for a dotted lookup such as ``A.x `` is in
@@ -563,7 +564,7 @@ The full C implementation can be found in :c:func:`type_getattro()` and
563
564
:c:func: `_PyType_Lookup() ` in :source: `Objects/typeobject.c `.
564
565
565
566
566
- Invocation from Super
567
+ Invocation from super
567
568
---------------------
568
569
569
570
The logic for super's dotted lookup is in the :meth: `__getattribute__ ` method for
@@ -580,7 +581,7 @@ The full C implementation can be found in :c:func:`super_getattro()` in
580
581
<https://www.python.org/download/releases/2.2.3/descrintro/#cooperation> `_.
581
582
582
583
583
- Summary of Invocation Logic
584
+ Summary of invocation logic
584
585
---------------------------
585
586
586
587
The mechanism for descriptors is embedded in the :meth: `__getattribute__() `
@@ -606,7 +607,7 @@ The important points to remember are:
606
607
* Non-data descriptors may be overridden by instance dictionaries.
607
608
608
609
609
- Automatic Name Notification
610
+ Automatic name notification
610
611
---------------------------
611
612
612
613
Sometimes it is desirable for a descriptor to know what class variable name it
@@ -624,7 +625,7 @@ place at the time of class creation. If descriptors are added to the class
624
625
afterwards, :meth: `__set_name__ ` will need to be called manually.
625
626
626
627
627
- ORM Example
628
+ ORM example
628
629
-----------
629
630
630
631
The following code is simplified skeleton showing how data descriptors could
@@ -694,8 +695,8 @@ Pure Python Equivalents
694
695
695
696
The descriptor protocol is simple and offers exciting possibilities. Several
696
697
use cases are so common that they have been prepackaged into built-in tools.
697
- Properties, bound methods, static methods, and class methods are all based on
698
- the descriptor protocol.
698
+ Properties, bound methods, static methods, class methods, and \_\_ slots \_\_ are
699
+ all based on the descriptor protocol.
699
700
700
701
701
702
Properties
@@ -774,7 +775,7 @@ to wrap access to the value attribute in a property data descriptor::
774
775
return self._value
775
776
776
777
777
- Functions and Methods
778
+ Functions and methods
778
779
---------------------
779
780
780
781
Python's object oriented features are built upon a function based environment.
@@ -858,7 +859,7 @@ If you have ever wondered where *self* comes from in regular methods or where
858
859
*cls * comes from in class methods, this is it!
859
860
860
861
861
- Static Methods
862
+ Static methods
862
863
--------------
863
864
864
865
Non-data descriptors provide a simple mechanism for variations on the usual
@@ -926,7 +927,7 @@ Using the non-data descriptor protocol, a pure Python version of
926
927
return self.f
927
928
928
929
929
- Class Methods
930
+ Class methods
930
931
-------------
931
932
932
933
Unlike static methods, class methods prepend the class reference to the
@@ -991,8 +992,8 @@ For example, a classmethod and property could be chained together::
991
992
def __doc__(cls):
992
993
return f'A doc for {cls.__name__!r}'
993
994
994
- Member Objects
995
- --------------
995
+ Member objects and __slots__
996
+ ----------------------------
996
997
997
998
When a class defines ``__slots__ ``, it replaces instance dictionaries with a
998
999
fixed-length array of slot values. From a user point of view that has
0 commit comments