Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
6eecf458
Commit
6eecf458
authored
Jun 25, 2003
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
* Document how descriptors are invoked.
* Fix minor parenthesis matching errors in ref3.tex.
parent
44f96166
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
63 additions
and
3 deletions
+63
-3
Doc/ref/ref3.tex
Doc/ref/ref3.tex
+63
-3
No files found.
Doc/ref/ref3.tex
View file @
6eecf458
...
...
@@ -481,8 +481,8 @@ function).
Special read-only attributes:
\member
{
im
_
self
}
is the class instance
object,
\member
{
im
_
func
}
is the function object;
\member
{
im
_
class
}
is the class of
\member
{
im
_
self
}
for bound methods
,
or the class that asked for the method for unbound methods
)
;
\member
{
im
_
class
}
is the class of
\member
{
im
_
self
}
for bound methods
or the class that asked for the method for unbound methods;
\member
{__
doc
__}
is the method's documentation (same as
\code
{
im
_
func.
__
doc
__}
);
\member
{__
name
__}
is the method name (same as
\code
{
im
_
func.
__
name
__}
);
\member
{__
module
__}
is the name of the
...
...
@@ -907,7 +907,7 @@ except clause or with a finally clause.
Slice objects are used to represent slices when
\emph
{
extended slice
syntax
}
is used. This is a slice using two colons, or multiple slices
or ellipses separated by commas, e.g.,
\code
{
a[i:j:step]
}
,
\code
{
a[i:j,
k:l]
}
, or
\code
{
a[..., i:j]
)
}
. They are also created by the built-in
k:l]
}
, or
\code
{
a[..., i:j]
}
. They are also created by the built-in
\function
{
slice()
}
\bifuncindex
{
slice
}
function.
Special read-only attributes:
\member
{
start
}
is the lower bound;
...
...
@@ -1249,6 +1249,66 @@ owner class.
\end{methoddesc}
\subsubsection
{
Invoking Descriptors
\label
{
descriptor
_
invocation
}}
In general, a descriptor is an object attribute with ``binding behavior'',
one whose attribute access has been overridden by methods in the descriptor
protocol:
\method
{__
get
__}
,
\method
{__
set
__}
, and
\method
{__
delete
__}
.
If any of those methods are defined for an object, it is said to be a
descriptor.
The default behavior for attribute access is to get, set, or delete the
attribute from an object's dictionary. For instance,
\code
{
a.x
}
has a
lookup chain starting with
\code
{
a.
__
dict
__
['x']
}
, then
\code
{
type(a).
__
dict
__
['x']
}
, and continuing
through the base classes of
\code
{
type(a)
}
excluding metaclasses.
However, if the looked-up value is an object defining one of the descriptor
methods, then Python may override the default behavior and invoke the
descriptor method instead. Where this occurs in the precedence chain depends
on which descriptor methods were defined and how they were called. Note that
descriptors are only invoked for new style objects or classes
(ones that subclass
\class
{
object
}
or
\class
{
type
}
).
The starting point for descriptor invocation is a binding,
\code
{
a.x
}
.
How the arguments are assembled depends on
\code
{
a
}
:
\begin{itemize}
\item
[Direct Call]
The simplest and least common call is when user code
directly invokes a descriptor method:
\code
{
x.
__
get
__
(a)
}
.
\item
[Instance Binding]
If binding to a new-style object instance,
\code
{
a.x
}
is transformed into the call:
\code
{
type(a).
__
dict
__
['x'].
__
get
__
(a, type(a))
}
.
\item
[Class Binding]
If binding to a new-style class,
\code
{
A.x
}
is transformed into the call:
\code
{
A.
__
dict
__
['x'].
__
get
__
(None, A)
}
.
\item
[Super Binding]
If
\code
{
a
}
is an instance of
\class
{
super
}
,
then the binding
\code
{
super(B, obj).m()
}
searches
\code
{
obj.
__
class
__
.
__
mro
__}
for the base class
\code
{
A
}
immediately
preceding
\code
{
B
}
and then invokes the descriptor with the call:
\code
{
A.
__
dict
__
['m'].
__
get
__
(obj, A)
}
.
\end{itemize}
For instance bindings, the precedence of descriptor invocation depends
on the which descriptor methods are defined. Data descriptors define
both
\method
{__
get
__}
and
\method
{__
set
__}
. Non-data descriptors have
just the
\method
{__
get
__}
method. Data descriptors always override
a redefinition in an instance dictionary. In contrast, non-data
descriptors can be overridden by instances.
Python methods (including
\function
{
staticmethod
}
and
\function
{
classmethod
}
)
are implemented as non-data descriptors. Accordingly, instances can
redefine and override methods. This allows individual instances to acquire
behaviors that differ from other instances of the same class.
The
\function
{
property
}
function is implemented as a data descriptor.
Accordingly, instances cannot override the behavior a property.
\subsection
{
Emulating callable objects
\label
{
callable-types
}}
\begin{methoddesc}
[object]
{__
call
__}{
self
\optional
{
, args...
}}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment