Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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
Modern style uses "cls" instead of "klass"
  • Loading branch information
rhettinger committed Oct 23, 2020
commit 7f52aee1628589be18c4eaa5047d53583ab9bffa
20 changes: 10 additions & 10 deletions 20 Doc/howto/descriptor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
Copy link
Contributor

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) if self.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 calling f.__get__ is incomplete and should instead call self.f.__get__(cls, cls).

def newfunc(*args):
return self.f(klass, *args)
return self.f(cls, *args)
return newfunc
Comment on lines 875 to 877
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using newfunc, wouldn't it be better to return types.MethodType(self.f, cls) similar to what is done in Function?


Morty Proxy This is a proxified and sanitized view of the page, visit original site.