10000 Create a primer section for the descriptor howto guide by rhettinger · Pull Request #22906 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Create a primer section for the descriptor howto guide #22906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 33 commits into from
Oct 23, 2020
Merged
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4285090
Simplest example: Return a constant
rhettinger Oct 22, 2020
fc4865a
Add directory size example of a dynamic descriptor
rhettinger Oct 23, 2020
83d7240
Add the managed attributes example
rhettinger Oct 23, 2020
6e1bae3
Add stub sections
rhettinger Oct 23, 2020
5cd1b8d
Fix hardwired value
rhettinger Oct 23, 2020
e8db254
Add the set_name example.
rhettinger Oct 23, 2020
3624731
Add the data validators complete practical example.
rhettinger Oct 23, 2020
dab7c25
Minor touch-ups
rhettinger Oct 23, 2020
95e1513
Add technical section for __set_name__
rhettinger Oct 23, 2020
65a0ab3
Line wrap
rhettinger Oct 23, 2020
8e5f962
Use the @-notation where possible
rhettinger Oct 23, 2020
7f52aee
Modern style uses "cls" instead of "klass"
rhettinger Oct 23, 2020
48329d2
Fix typo
rhettinger Oct 23, 2020
1583c25
Missing colon
rhettinger Oct 23, 2020
45466fc
Note false positive in the suspcious entry extension
rhettinger Oct 23, 2020
31fb230
Note false positive in the suspcious entry extension
rhettinger Oct 23, 2020
58275e7
Note false positive in the suspcious entry extension
rhettinger Oct 23, 2020
80bfd08
Note false positive in the suspcious entry extension
rhettinger Oct 23, 2020
112a272
Fix typos. Minor grammar edits.
rhettinger Oct 23, 2020
1a899c8
Fix method name
rhettinger Oct 23, 2020
b3934e8
Fix SyntaxError and misspelled predicate name
rhettinger Oct 23, 2020
90992bf
Add references to and from the glossary
rhettinger Oct 23, 2020
2599cff
Fix markup
rhettinger Oct 23, 2020
11e790a
Update Doc/howto/descriptor.rst
rhettinger Oct 23, 2020
b0c435c
Minor comment and variable name improvements
rhettinger Oct 23, 2020
0f6df85
Simplify examples. Add closing thoughts section.
rhettinger Oct 23, 2020
1e9482e
Add more section headings
rhettinger Oct 23, 2020
512fa4b
More wordsmithing
rhettinger Oct 23, 2020
0dc5f72
Wrap long lines
rhettinger Oct 23, 2020
10b9cd4
Clarify the motivation and the caller/callee relationship
rhettinger Oct 23, 2020
12b0fa2
Beautify technical tutorial sections
rhettinger Oct 23, 2020
8ca8c77
Move comments of the the code and into the main text
rhettinger Oct 23, 2020
cc22b88
Remove outdated references to Python 2 and new-style classes
rhettinger Oct 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Minor touch-ups
  • Loading branch information
rhettinger committed Oct 23, 2020
commit dab7c25a08bf6de2de6fa12cd7ff3b05235a7de5
24 changes: 15 additions & 9 deletions Doc/howto/descriptor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,31 @@ Descriptor HowTo Guide

.. Contents::

Preview
^^^^^^^

Descriptors let objects customize attribute lookup, storage, and deletion.

This howto guide has three major sections. It starts with a "primer" that gives a basic overview. The second section explains a complete, practical descriptor example. The third section provides a more technical tutorial that goes into the detailed mechanics of how descriptors work.
This HowTo guide has three major sections:

1) The "primer" gives a basic overview, moving gently from simple examples, adding one feature at a time. It is a great place to start.

2) The second section shows a complete, practical descriptor example. If you already know the basics, start there.

3) The third section provides a more technical tutorial that goes into the detailed mechanics of how descriptors work. Most people don't need this level of detal.


Primer
^^^^^^

In this primer, we start with most basic possible example and then we'll add new capabilities one by one.


Simple example: A descriptor that returns a constant
----------------------------------------------------

The :class:`Ten` class is a descriptor that always returns a constant ``10``::


class Ten:

def __get__(self, obj, objtype=None):
return 10

Expand All @@ -48,7 +53,8 @@ In the ``a.x`` attribute lookup, the dot operator finds the value ``5`` stored i

This example shows how a simple descriptor works, but it isn't very useful. For retrieving constants, normal attribute lookup would almost always be better.

In the next section, we'll create something most useful, a dynamic lookup.
In the next section, we'll create something more useful, a dynamic lookup.


Dynamic lookups
---------------
Expand Down Expand Up @@ -147,6 +153,7 @@ An interactive session shows that all access to the managed attribute *age* is l

One major issue with this example is the private name *_age* is hardwired in the *LoggedAccess* class. That means that each instance can only have one logged attribute and that its name is unchangeable. In the next example, we'll fix that problem.


Customized Names
----------------

Expand Down Expand Up @@ -209,7 +216,6 @@ The two *Person* instances contain only the private names::
{'_name': 'Catherine C', '_age': 20}



Complete Practical Example
^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down Expand Up @@ -574,7 +580,7 @@ affect existing client code accessing the attribute directly. The solution is
to wrap access to the value attribute in a property data descriptor::

class Cell:
. . .
...
def getvalue(self):
"Recalculate the cell before returning value"
self.recalc()
Expand All @@ -601,7 +607,7 @@ non-data descriptors which return bound methods when they are invoked from an
object. In pure Python, it works like this::

class Function:
. . .
...
def __get__(self, obj, objtype=None):
"Simulate func_descr_get() in Objects/funcobject.c"
if obj is None:
Expand Down Expand Up @@ -732,7 +738,7 @@ is to create alternate class constructors. In Python 2.3, the classmethod
Python equivalent is::

class Dict:
. . .
...
def fromkeys(klass, iterable, value=None):
"Emulate dict_fromkeys() in Objects/dictobject.c"
d = klass()
Expand Down
0