Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="ref.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="ref.html" title='Python Reference Manual' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="slots.html" /> | |
13 | <link rel="prev" href="descriptors.html" /> | |
14 | <link rel="parent" href="attribute-access.html" /> | |
15 | <link rel="next" href="slots.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>3.3.2.3 Invoking Descriptors </title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="3.3.2.2 Implementing Descriptors" | |
25 | href="descriptors.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="3.3.2 Customizing attribute access" | |
28 | href="attribute-access.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="3.3.2.4 __slots__" | |
31 | href="slots.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Reference Manual</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><img src='../icons/blank.png' | |
38 | border='0' height='32' alt='' width='32' /></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="descriptors.html">3.3.2.2 Implementing Descriptors</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="attribute-access.html">3.3.2 Customizing attribute access</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="slots.html">3.3.2.4 __slots__</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H3><A NAME="SECTION005323000000000000000"></A><A NAME="descriptor-invocation"></A> | |
56 | <BR> | |
57 | 3.3.2.3 Invoking Descriptors | |
58 | </H3> | |
59 | ||
60 | <P> | |
61 | In general, a descriptor is an object attribute with ``binding behavior'', | |
62 | one whose attribute access has been overridden by methods in the descriptor | |
63 | protocol: <tt class="method">__get__()</tt>, <tt class="method">__set__()</tt>, and <tt class="method">__delete__()</tt>. | |
64 | If any of those methods are defined for an object, it is said to be a | |
65 | descriptor. | |
66 | ||
67 | <P> | |
68 | The default behavior for attribute access is to get, set, or delete the | |
69 | attribute from an object's dictionary. For instance, <code>a.x</code> has a | |
70 | lookup chain starting with <code>a.__dict__['x']</code>, then | |
71 | <code>type(a).__dict__['x']</code>, and continuing | |
72 | through the base classes of <code>type(a)</code> excluding metaclasses. | |
73 | ||
74 | <P> | |
75 | However, if the looked-up value is an object defining one of the descriptor | |
76 | methods, then Python may override the default behavior and invoke the | |
77 | descriptor method instead. Where this occurs in the precedence chain depends | |
78 | on which descriptor methods were defined and how they were called. Note that | |
79 | descriptors are only invoked for new style objects or classes | |
80 | (ones that subclass <tt class="class">object()</tt> or <tt class="class">type()</tt>). | |
81 | ||
82 | <P> | |
83 | The starting point for descriptor invocation is a binding, <code>a.x</code>. | |
84 | How the arguments are assembled depends on <code>a</code>: | |
85 | ||
86 | <P> | |
87 | <DL COMPACT> | |
88 | <DT>Direct Call</DT> | |
89 | <DD>The simplest and least common call is when user code | |
90 | directly invokes a descriptor method: <code>x.__get__(a)</code>. | |
91 | ||
92 | <P> | |
93 | </DD> | |
94 | <DT>Instance Binding</DT> | |
95 | <DD>If binding to a new-style object instance, | |
96 | <code>a.x</code> is transformed into the call: | |
97 | <code>type(a).__dict__['x'].__get__(a, type(a))</code>. | |
98 | ||
99 | <P> | |
100 | </DD> | |
101 | <DT>Class Binding</DT> | |
102 | <DD>If binding to a new-style class, <code>A.x</code> | |
103 | is transformed into the call: <code>A.__dict__['x'].__get__(None, A)</code>. | |
104 | ||
105 | <P> | |
106 | </DD> | |
107 | <DT>Super Binding</DT> | |
108 | <DD>If <code>a</code> is an instance of <tt class="class">super</tt>, | |
109 | then the binding <code>super(B, obj).m()</code> searches | |
110 | <code>obj.__class__.__mro__</code> for the base class <code>A</code> immediately | |
111 | preceding <code>B</code> and then invokes the descriptor with the call: | |
112 | <code>A.__dict__['m'].__get__(obj, A)</code>. | |
113 | ||
114 | <P> | |
115 | </DD> | |
116 | </DL> | |
117 | ||
118 | <P> | |
119 | For instance bindings, the precedence of descriptor invocation depends | |
120 | on the which descriptor methods are defined. Data descriptors define | |
121 | both <tt class="method">__get__()</tt> and <tt class="method">__set__()</tt>. Non-data descriptors have | |
122 | just the <tt class="method">__get__()</tt> method. Data descriptors always override | |
123 | a redefinition in an instance dictionary. In contrast, non-data | |
124 | descriptors can be overridden by instances. | |
125 | ||
126 | <P> | |
127 | Python methods (including <tt class="function">staticmethod()</tt> and <tt class="function">classmethod()</tt>) | |
128 | are implemented as non-data descriptors. Accordingly, instances can | |
129 | redefine and override methods. This allows individual instances to acquire | |
130 | behaviors that differ from other instances of the same class. | |
131 | ||
132 | <P> | |
133 | The <tt class="function">property()</tt> function is implemented as a data descriptor. | |
134 | Accordingly, instances cannot override the behavior of a property. | |
135 | ||
136 | <P> | |
137 | ||
138 | <DIV CLASS="navigation"> | |
139 | <div class='online-navigation'> | |
140 | <p></p><hr /> | |
141 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
142 | <tr> | |
143 | <td class='online-navigation'><a rel="prev" title="3.3.2.2 Implementing Descriptors" | |
144 | href="descriptors.html"><img src='../icons/previous.png' | |
145 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
146 | <td class='online-navigation'><a rel="parent" title="3.3.2 Customizing attribute access" | |
147 | href="attribute-access.html"><img src='../icons/up.png' | |
148 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
149 | <td class='online-navigation'><a rel="next" title="3.3.2.4 __slots__" | |
150 | href="slots.html"><img src='../icons/next.png' | |
151 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
152 | <td align="center" width="100%">Python Reference Manual</td> | |
153 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
154 | href="contents.html"><img src='../icons/contents.png' | |
155 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
156 | <td class='online-navigation'><img src='../icons/blank.png' | |
157 | border='0' height='32' alt='' width='32' /></td> | |
158 | <td class='online-navigation'><a rel="index" title="Index" | |
159 | href="genindex.html"><img src='../icons/index.png' | |
160 | border='0' height='32' alt='Index' width='32' /></A></td> | |
161 | </tr></table> | |
162 | <div class='online-navigation'> | |
163 | <b class="navlabel">Previous:</b> | |
164 | <a class="sectref" rel="prev" href="descriptors.html">3.3.2.2 Implementing Descriptors</A> | |
165 | <b class="navlabel">Up:</b> | |
166 | <a class="sectref" rel="parent" href="attribute-access.html">3.3.2 Customizing attribute access</A> | |
167 | <b class="navlabel">Next:</b> | |
168 | <a class="sectref" rel="next" href="slots.html">3.3.2.4 __slots__</A> | |
169 | </div> | |
170 | </div> | |
171 | <hr /> | |
172 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
173 | </DIV> | |
174 | <!--End of Navigation Panel--> | |
175 | <ADDRESS> | |
176 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
177 | </ADDRESS> | |
178 | </BODY> | |
179 | </HTML> |