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="prev" href="numeric-types.html" /> | |
13 | <link rel="parent" href="specialnames.html" /> | |
14 | <link rel="next" href="execmodel.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>3.3.8 Coercion rules</title> | |
17 | </head> | |
18 | <body> | |
19 | <DIV CLASS="navigation"> | |
20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
22 | <tr> | |
23 | <td class='online-navigation'><a rel="prev" title="3.3.7 Emulating numeric types" | |
24 | href="numeric-types.html"><img src='../icons/previous.png' | |
25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
26 | <td class='online-navigation'><a rel="parent" title="3.3 Special method names" | |
27 | href="specialnames.html"><img src='../icons/up.png' | |
28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
29 | <td class='online-navigation'><a rel="next" title="4. Execution model" | |
30 | href="execmodel.html"><img src='../icons/next.png' | |
31 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
32 | <td align="center" width="100%">Python Reference Manual</td> | |
33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
34 | href="contents.html"><img src='../icons/contents.png' | |
35 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
36 | <td class='online-navigation'><img src='../icons/blank.png' | |
37 | border='0' height='32' alt='' width='32' /></td> | |
38 | <td class='online-navigation'><a rel="index" title="Index" | |
39 | href="genindex.html"><img src='../icons/index.png' | |
40 | border='0' height='32' alt='Index' width='32' /></A></td> | |
41 | </tr></table> | |
42 | <div class='online-navigation'> | |
43 | <b class="navlabel">Previous:</b> | |
44 | <a class="sectref" rel="prev" href="numeric-types.html">3.3.7 Emulating numeric types</A> | |
45 | <b class="navlabel">Up:</b> | |
46 | <a class="sectref" rel="parent" href="specialnames.html">3.3 Special method names</A> | |
47 | <b class="navlabel">Next:</b> | |
48 | <a class="sectref" rel="next" href="execmodel.html">4. Execution model</A> | |
49 | </div> | |
50 | <hr /></div> | |
51 | </DIV> | |
52 | <!--End of Navigation Panel--> | |
53 | ||
54 | <H2><A NAME="SECTION005380000000000000000"></A><A NAME="coercion-rules"></A> | |
55 | <BR> | |
56 | 3.3.8 Coercion rules | |
57 | </H2> | |
58 | ||
59 | <P> | |
60 | This section used to document the rules for coercion. As the language | |
61 | has evolved, the coercion rules have become hard to document | |
62 | precisely; documenting what one version of one particular | |
63 | implementation does is undesirable. Instead, here are some informal | |
64 | guidelines regarding coercion. In Python 3.0, coercion will not be | |
65 | supported. | |
66 | ||
67 | <P> | |
68 | ||
69 | <UL> | |
70 | <LI><P> | |
71 | If the left operand of a % operator is a string or Unicode object, no | |
72 | coercion takes place and the string formatting operation is invoked | |
73 | instead. | |
74 | ||
75 | <P> | |
76 | </LI> | |
77 | <LI><P> | |
78 | It is no longer recommended to define a coercion operation. | |
79 | Mixed-mode operations on types that don't define coercion pass the | |
80 | original arguments to the operation. | |
81 | ||
82 | <P> | |
83 | </LI> | |
84 | <LI><P> | |
85 | New-style classes (those derived from <tt class="class">object</tt>) never invoke the | |
86 | <tt class="method">__coerce__()</tt> method in response to a binary operator; the only | |
87 | time <tt class="method">__coerce__()</tt> is invoked is when the built-in function | |
88 | <tt class="function">coerce()</tt> is called. | |
89 | ||
90 | <P> | |
91 | </LI> | |
92 | <LI><P> | |
93 | For most intents and purposes, an operator that returns | |
94 | <code>NotImplemented</code> is treated the same as one that is not | |
95 | implemented at all. | |
96 | ||
97 | <P> | |
98 | </LI> | |
99 | <LI><P> | |
100 | Below, <tt class="method">__op__()</tt> and <tt class="method">__rop__()</tt> are used to signify | |
101 | the generic method names corresponding to an operator; | |
102 | <tt class="method">__iop__()</tt> is used for the corresponding in-place operator. For | |
103 | example, for the operator `<code>+</code>', <tt class="method">__add__()</tt> and | |
104 | <tt class="method">__radd__()</tt> are used for the left and right variant of the | |
105 | binary operator, and <tt class="method">__iadd__()</tt> for the in-place variant. | |
106 | ||
107 | <P> | |
108 | </LI> | |
109 | <LI><P> | |
110 | For objects <var>x</var> and <var>y</var>, first <code><var>x</var>.__op__(<var>y</var>)</code> | |
111 | is tried. If this is not implemented or returns <code>NotImplemented</code>, | |
112 | <code><var>y</var>.__rop__(<var>x</var>)</code> is tried. If this is also not | |
113 | implemented or returns <code>NotImplemented</code>, a <tt class="exception">TypeError</tt> | |
114 | exception is raised. But see the following exception: | |
115 | ||
116 | <P> | |
117 | </LI> | |
118 | <LI><P> | |
119 | Exception to the previous item: if the left operand is an instance of | |
120 | a built-in type or a new-style class, and the right operand is an | |
121 | instance of a proper subclass of that type or class, the right | |
122 | operand's <tt class="method">__rop__()</tt> method is tried <em>before</em> the left | |
123 | operand's <tt class="method">__op__()</tt> method. This is done so that a subclass can | |
124 | completely override binary operators. Otherwise, the left operand's | |
125 | __op__ method would always accept the right operand: when an instance | |
126 | of a given class is expected, an instance of a subclass of that class | |
127 | is always acceptable. | |
128 | ||
129 | <P> | |
130 | </LI> | |
131 | <LI><P> | |
132 | When either operand type defines a coercion, this coercion is called | |
133 | before that type's <tt class="method">__op__()</tt> or <tt class="method">__rop__()</tt> method is | |
134 | called, but no sooner. If the coercion returns an object of a | |
135 | different type for the operand whose coercion is invoked, part of the | |
136 | process is redone using the new object. | |
137 | ||
138 | <P> | |
139 | </LI> | |
140 | <LI><P> | |
141 | When an in-place operator (like `<code>+=</code>') is used, if the left | |
142 | operand implements <tt class="method">__iop__()</tt>, it is invoked without any | |
143 | coercion. When the operation falls back to <tt class="method">__op__()</tt> and/or | |
144 | <tt class="method">__rop__()</tt>, the normal coercion rules apply. | |
145 | ||
146 | <P> | |
147 | </LI> | |
148 | <LI><P> | |
149 | In <var>x</var><code>+</code><var>y</var>, if <var>x</var> is a sequence that implements | |
150 | sequence concatenation, sequence concatenation is invoked. | |
151 | ||
152 | <P> | |
153 | </LI> | |
154 | <LI><P> | |
155 | In <var>x</var><code>*</code><var>y</var>, if one operator is a sequence that | |
156 | implements sequence repetition, and the other is an integer | |
157 | (<tt class="class">int</tt> or <tt class="class">long</tt>), sequence repetition is invoked. | |
158 | ||
159 | <P> | |
160 | </LI> | |
161 | <LI><P> | |
162 | Rich comparisons (implemented by methods <tt class="method">__eq__()</tt> and so on) | |
163 | never use coercion. Three-way comparison (implemented by | |
164 | <tt class="method">__cmp__()</tt>) does use coercion under the same conditions as | |
165 | other binary operations use it. | |
166 | ||
167 | <P> | |
168 | </LI> | |
169 | <LI><P> | |
170 | In the current implementation, the built-in numeric types <tt class="class">int</tt>, | |
171 | <tt class="class">long</tt> and <tt class="class">float</tt> do not use coercion; the type | |
172 | <tt class="class">complex</tt> however does use it. The difference can become | |
173 | apparent when subclassing these types. Over time, the type | |
174 | <tt class="class">complex</tt> may be fixed to avoid coercion. All these types | |
175 | implement a <tt class="method">__coerce__()</tt> method, for use by the built-in | |
176 | <tt class="function">coerce()</tt> function. | |
177 | ||
178 | <P> | |
179 | </LI> | |
180 | </UL> | |
181 | ||
182 | <DIV CLASS="navigation"> | |
183 | <div class='online-navigation'> | |
184 | <p></p><hr /> | |
185 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
186 | <tr> | |
187 | <td class='online-navigation'><a rel="prev" title="3.3.7 Emulating numeric types" | |
188 | href="numeric-types.html"><img src='../icons/previous.png' | |
189 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
190 | <td class='online-navigation'><a rel="parent" title="3.3 Special method names" | |
191 | href="specialnames.html"><img src='../icons/up.png' | |
192 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
193 | <td class='online-navigation'><a rel="next" title="4. Execution model" | |
194 | href="execmodel.html"><img src='../icons/next.png' | |
195 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
196 | <td align="center" width="100%">Python Reference Manual</td> | |
197 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
198 | href="contents.html"><img src='../icons/contents.png' | |
199 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
200 | <td class='online-navigation'><img src='../icons/blank.png' | |
201 | border='0' height='32' alt='' width='32' /></td> | |
202 | <td class='online-navigation'><a rel="index" title="Index" | |
203 | href="genindex.html"><img src='../icons/index.png' | |
204 | border='0' height='32' alt='Index' width='32' /></A></td> | |
205 | </tr></table> | |
206 | <div class='online-navigation'> | |
207 | <b class="navlabel">Previous:</b> | |
208 | <a class="sectref" rel="prev" href="numeric-types.html">3.3.7 Emulating numeric types</A> | |
209 | <b class="navlabel">Up:</b> | |
210 | <a class="sectref" rel="parent" href="specialnames.html">3.3 Special method names</A> | |
211 | <b class="navlabel">Next:</b> | |
212 | <a class="sectref" rel="next" href="execmodel.html">4. Execution model</A> | |
213 | </div> | |
214 | </div> | |
215 | <hr /> | |
216 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
217 | </DIV> | |
218 | <!--End of Navigation Panel--> | |
219 | <ADDRESS> | |
220 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
221 | </ADDRESS> | |
222 | </BODY> | |
223 | </HTML> |