-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
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 fc4865a
Add directory size example of a dynamic descriptor
rhettinger 83d7240
Add the managed attributes example
rhettinger 6e1bae3
Add stub sections
rhettinger 5cd1b8d
Fix hardwired value
rhettinger e8db254
Add the set_name example.
rhettinger 3624731
Add the data validators complete practical example.
rhettinger dab7c25
Minor touch-ups
rhettinger 95e1513
Add technical section for __set_name__
rhettinger 65a0ab3
Line wrap
rhettinger 8e5f962
Use the @-notation where possible
rhettinger 7f52aee
Modern style uses "cls" instead of "klass"
rhettinger 48329d2
Fix typo
rhettinger 1583c25
Missing colon
rhettinger 45466fc
Note false positive in the suspcious entry extension
rhettinger 31fb230
Note false positive in the suspcious entry extension
rhettinger 58275e7
Note false positive in the suspcious entry extension
rhettinger 80bfd08
Note false positive in the suspcious entry extension
rhettinger 112a272
Fix typos. Minor grammar edits.
rhettinger 1a899c8
Fix method name
rhettinger b3934e8
Fix SyntaxError and misspelled predicate name
rhettinger 90992bf
Add references to and from the glossary
rhettinger 2599cff
Fix markup
rhettinger 11e790a
Update Doc/howto/descriptor.rst
rhettinger b0c435c
Minor comment and variable name improvements
rhettinger 0f6df85
Simplify examples. Add closing thoughts section.
rhettinger 1e9482e
Add more section headings
rhettinger 512fa4b
More wordsmithing
rhettinger 0dc5f72
Wrap long lines
rhettinger 10b9cd4
Clarify the motivation and the caller/callee relationship
rhettinger 12b0fa2
Beautify technical tutorial sections
rhettinger 8ca8c77
Move comments of the the code and into the main text
rhettinger cc22b88
Remove outdated references to Python 2 and new-style classes
rhettinger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Modern style uses "cls" instead of "klass"
- Loading branch information
commit 7f52aee1628589be18c4eaa5047d53583ab9bffa
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -709,7 +709,7 @@ patterns of binding functions into methods. | |
|
||
To recap, functions have a :meth:`__get__` method so that they can be converted | ||
to a method when accessed as attributes. The non-data descriptor transforms an | ||
``obj.f(*args)`` call into ``f(obj, *args)``. Calling ``klass.f(*args)`` | ||
``obj.f(*args)`` call into ``f(obj, *args)``. Calling ``cls.f(*args)`` | ||
becomes ``f(*args)``. | ||
|
||
This chart summarizes the binding and its two most useful variants: | ||
|
@@ -722,7 +722,7 @@ This chart summarizes the binding and its two most useful variants: | |
+-----------------+----------------------+------------------+ | ||
| staticmethod | f(\*args) | f(\*args) | | ||
+-----------------+----------------------+------------------+ | ||
| classmethod | f(type(obj), \*args) | f(klass, \*args) | | ||
| classmethod | f(type(obj), \*args) | f(cls, \*args) | | ||
+-----------------+----------------------+------------------+ | ||
|
||
Static methods return the underlying function without changes. Calling either | ||
|
@@ -774,8 +774,8 @@ for whether the caller is an object or a class:: | |
|
||
>>> class E: | ||
... @classmethod | ||
... def f(klass, x): | ||
... return klass.__name__, x | ||
... def f(cls, x): | ||
... return cls.__name__, x | ||
... | ||
>>> print(E.f(3)) | ||
('E', 3) | ||
|
@@ -793,9 +793,9 @@ Python equivalent is:: | |
... | ||
|
||
@classmethod | ||
def fromkeys(klass, iterable, value=None): | ||
def fromkeys(cls, iterable, value=None): | ||
"Emulate dict_fromkeys() in Objects/dictobject.c" | ||
d = klass() | ||
d = cls() | ||
for key in iterable: | ||
d[key] = value | ||
return d | ||
|
@@ -814,10 +814,10 @@ Using the non-data descriptor protocol, a pure Python version of | |
def __init__(self, f): | ||
self.f = f | ||
|
||
def __get__(self, obj, klass=None): | ||
if klass is None: | ||
klass = type(obj) | ||
def __get__(self, obj, cls=None): | ||
if cls is None: | ||
cls = type(obj) | ||
def newfunc(*args): | ||
return self.f(klass, *args) | ||
return self.f(cls, *args) | ||
return newfunc | ||
Comment on lines
875
to
877
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of using |
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The pure Python version doesn't capture the change #8405, which will call
self.f.__get__(cls)
ifself.f
has__get__
. I think this behavior is surprising (and a little weird) and should be captured in this document, which doesn't shy away from nitty gritty details. I also happen to think this behavior of callingf.__get__
is incomplete and should instead callself.f.__get__(cls, cls)
.