4) The last section has pure Python equivalents for built-in descriptors that
are written in C. Read this if you're curious about how functions turn
- into bound methods or about how to implement common tools like
- :func:`classmethod`, :func:`staticmethod`, and :func:`property`.
+ into bound methods or about the implementation of common tools like
+ :func:`classmethod`, :func:`staticmethod`, :func:`property`, and
+ :term:`__slots__`.
Primer
we'll fix that problem.
-Customized Names
+Customized names
----------------
When a class uses descriptors, it can inform each descriptor about what
elegance of its design.
-Definition and Introduction
+Definition and introduction
---------------------------
In general, a descriptor is an object attribute with "binding behavior", one
everyday Python programs.
-Descriptor Protocol
+Descriptor protocol
-------------------
``descr.__get__(self, obj, type=None) -> value``
placeholder is enough to make it a data descriptor.
-Overview of Descriptor Invocation
+Overview of descriptor invocation
---------------------------------
A descriptor can be called directly with ``desc.__get__(obj)`` or
instance of super.
-Invocation from an Instance
+Invocation from an instance
---------------------------
Instance lookup scans through a chain of namespaces giving data descriptors
doesn't exist when its class defines :term:`__slots__`.
-Invocation from a Class
+Invocation from a class
-----------------------
The logic for a dotted lookup such as ``A.x`` is in
:c:func:`_PyType_Lookup()` in :source:`Objects/typeobject.c`.
-Invocation from Super
+Invocation from super
---------------------
The logic for super's dotted lookup is in the :meth:`__getattribute__` method for
<https://www.python.org/download/releases/2.2.3/descrintro/#cooperation>`_.
-Summary of Invocation Logic
+Summary of invocation logic
---------------------------
The mechanism for descriptors is embedded in the :meth:`__getattribute__()`
* Non-data descriptors may be overridden by instance dictionaries.
-Automatic Name Notification
+Automatic name notification
---------------------------
Sometimes it is desirable for a descriptor to know what class variable name it
afterwards, :meth:`__set_name__` will need to be called manually.
-ORM Example
+ORM example
-----------
The following code is simplified skeleton showing how data descriptors could
The descriptor protocol is simple and offers exciting possibilities. Several
use cases are so common that they have been prepackaged into built-in tools.
-Properties, bound methods, static methods, and class methods are all based on
-the descriptor protocol.
+Properties, bound methods, static methods, class methods, and \_\_slots\_\_ are
+all based on the descriptor protocol.
Properties
return self._value
-Functions and Methods
+Functions and methods
---------------------
Python's object oriented features are built upon a function based environment.
*cls* comes from in class methods, this is it!
-Static Methods
+Static methods
--------------
Non-data descriptors provide a simple mechanism for variations on the usual
return self.f
-Class Methods
+Class methods
-------------
Unlike static methods, class methods prepend the class reference to the
def __doc__(cls):
return f'A doc for {cls.__name__!r}'
-Member Objects
---------------
+Member objects and __slots__
+----------------------------
When a class defines ``__slots__``, it replaces instance dictionaries with a
fixed-length array of slot values. From a user point of view that has