merged in Vitja's tab removals
[cython.git] / Doc / special_methods.html
1 <!DOCTYPE doctype PUBLIC "-//w3c//dtd html 4.0 transitional//en">
2 <html><head>
3          <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
4          <meta name="GENERATOR" content="Mozilla/4.61 (Macintosh; I; PPC) [Netscape]"><title>Special Methods of Extenstion Types</title></head>
5 <body>
6    <h1>  <hr width="100%">Special Methods of Extension Types  
7 <hr width="100%"></h1>
8   This page describes the special methods currently supported by Cython extension
9  types. A complete list of all the special methods appears in the table at 
10 the bottom. Some of these methods behave differently from their Python counterparts 
11 or have no direct Python counterparts, and require special mention.  
12 <p><span style="font-weight: bold;">Note:</span><i> Everything said on this page applies only to </i><span style="font-weight: bold;">extension</span><i style="font-weight: bold;">
13 </i><span style="font-weight: bold;">types</span><i>, defined with the </i><span style="font-weight: bold; font-family: monospace;">cdef class</span><i> statement. It doesn't apply&nbsp;
14 to classes defined with the Python </i><span style="font-family: monospace;">class</span><i><span style="font-family: monospace;"> </span>statement, where the normal
15  Python rules apply.</i> </p>
16  <h2><small>Declaration</small></h2>Special methods of extension types must be declared with <span style="font-family: monospace; font-weight: bold;">def</span>, <span style="font-style: italic;">not</span> <span style="font-family: monospace;">cdef</span>.<br>
17 <h2><font size="+1">Docstrings</font></h2>
18
19   Currently, docstrings are not fully supported in special methods of extension
20  types. You can place a docstring in the source to serve as a comment, but
21  it won't show up in the corresponding <span style="font-family: monospace;">__doc__</span> attribute at run time. (This
22  is a Python limitation -- there's nowhere in the PyTypeObject data structure
23  to put such docstrings.)  
24 <h2> <font size="+1">Initialisation methods: <tt>__new__</tt> and <tt>__init__</tt></font></h2>
25   There are two methods concerned with initialising the object<tt>.</tt>
26 <p>The <b><tt>__new__</tt></b> method is where you should perform basic C-level 
27 initialisation of the object, including allocation of any C data structures 
28 that your object will own. You need to be careful what you do in the __new__ 
29 method, because the object may not yet be a valid Python object when it is 
30 called. Therefore, you must not invoke any Python operations which might touch
31 the object; in particular, do not try to call any of its methods. </p>
32  <p>Unlike the corresponding method in Python, your <tt>__new__</tt> method
33  is <i>not</i> responsible for <i>creating</i> the object. By the time your
34  <tt>__new__</tt> method is called, memory has been allocated for the object 
35 and any C attributes it has have been initialised to 0 or null. (Any Python 
36 attributes have also been initialised to <tt>None</tt>, but you probably shouldn't
37 rely on that.) Your <tt>__new__</tt> method is guaranteed to be called exactly
38 once.<br>
39 <br>
40 If your extension type has a base type, the <tt>__new__</tt> method of the
41 base type is automatically called <i>before</i> your <tt>__new__</tt> method
42 is called; you cannot explicitly call the inherited <tt>__new__</tt> method.
43 If you need to pass a modified argument list to the base type, you will have
44 to do the relevant part of the initialisation in the <tt>__init__</tt> method
45 instead (where the normal rules for calling inherited methods apply).<br>
46  </p>
47  <p>Note that the first parameter of the <tt>__new__</tt> method is the object 
48 to be initialised, not the class of the object as it is in Python. </p>
49  <p>Any initialisation which cannot safely be done in the <tt>__new__</tt>
50 method should be done in the <b><tt>__init__</tt></b> method. By the time
51  <tt>__init__</tt> is called, the object is a fully valid Python object and 
52 all operations are safe. Under some circumstances it is possible for <tt>__init__</tt>
53 to be called more than once or not to be called at all, so your other methods
54  should be designed to be robust in such situations. </p>
55  <p>Keep in mind that any arguments passed to the constructor will be passed
56  to the <tt>__new__</tt> method as well as the <tt>__init__</tt> method.
57 If you anticipate subclassing your extension type in Python, you may find
58 it useful to give the <tt>__new__</tt> method * and ** arguments so that
59 it can accept and ignore extra arguments. Otherwise, any Python subclass
60 which has an <tt>__init__</tt> with a different signature will have to override 
61 <tt>__new__</tt> as well as <tt>__init__</tt>, which the writer of a Python 
62 class wouldn't expect to have to do. </p>
63  <h2> <font size="+1">Finalization method: <tt>__dealloc__</tt><tt></tt></font></h2>
64   The counterpart to the <tt>__new__</tt> method is the <b><tt>__dealloc__</tt></b>
65 method, which should perform the inverse of the <tt>__new__</tt> method.
66 Any C data structures that you allocated in your <tt>__new__</tt> method
67 should be freed in your <tt>__dealloc__</tt> method.  
68 <p>You need to be careful what you do in a <tt>__dealloc__</tt> method. By 
69 the time your <tt>__dealloc__</tt> method is called, the object may already 
70 have been partially destroyed and may not be in a valid state as far as Python 
71 is concerned, so you should avoid invoking any Python operations which might 
72 touch the object. In particular, don't call any other methods of the object 
73 or do anything which might cause the object to be resurrected. It's best if
74 you stick to just deallocating C data. </p>
75  <p>You don't need to worry about deallocating Python attributes of your object, 
76 because that will be done for you by Cython after your <tt>__dealloc__</tt>
77 method returns.<br>
78  <br>
79  <b>Note:</b> There is no <tt>__del__</tt> method for extension types. (Earlier 
80 versions of the Cython documentation stated that there was, but this turned 
81 out to be incorrect.)<br>
82   </p>
83  <h2><font size="+1">Arithmetic methods</font></h2>
84   Arithmetic operator methods, such as <tt>__add__</tt>, behave differently
85  from their Python counterparts. There are no separate "reversed" versions
86  of these methods (<tt>__radd__</tt>, etc.) Instead, if the first operand
87 cannot perform the operation, the <i>same</i> method of the second operand
88 is called, with the operands in the <i>same order</i>.  
89 <p>This means that you can't rely on the first parameter of these methods
90  being "self", and you should test the types of both operands before deciding
91  what to do. If you can't handle the combination of types you've been given,
92  you should return <tt>NotImplemented</tt>. </p>
93  <p>This also applies to the in-place arithmetic method <tt>__ipow__</tt>.
94  It doesn't apply to any of the <i>other</i> in-place methods (<tt>__iadd__</tt>,
95  etc.) which always take self as the first argument. </p>
96  <h2> <font size="+1">Rich comparisons</font></h2>
97   There are no separate methods for the individual rich comparison operations
98  (<tt>__eq__</tt>, <tt>__le__</tt>, etc.) Instead there is a single method
99  <tt>__richcmp__</tt> which takes an integer indicating which operation is 
100 to be performed, as follows:  
101 <ul>
102       <ul>
103  &nbsp;         <table nosave="" border="0" cellpadding="5" cellspacing="0">
104   <tbody>
105          <tr nosave="">
106   <td nosave="" bgcolor="#ffcc33" width="30">                     <div align="right">&lt;</div>
107   </td>
108    <td nosave="" bgcolor="#66ffff" width="30">0</td>
109    <td><br>
110            </td>
111    <td nosave="" bgcolor="#ffcc33" width="30">                     <div align="right">==</div>
112   </td>
113    <td nosave="" bgcolor="#66ffff" width="30">2</td>
114    <td><br>
115            </td>
116    <td nosave="" bgcolor="#ffcc33" width="30">                     <div align="right">&gt;</div>
117   </td>
118    <td nosave="" bgcolor="#66ffff" width="30">4</td>
119   </tr>
120    <tr nosave="">
121   <td nosave="" bgcolor="#ffcc33">                     <div align="right">&lt;=</div>
122   </td>
123    <td nosave="" bgcolor="#66ffff">1</td>
124    <td><br>
125            </td>
126    <td nosave="" bgcolor="#ffcc33">                     <div align="right">!=</div>
127   </td>
128    <td nosave="" bgcolor="#66ffff">3</td>
129    <td><br>
130            </td>
131    <td nosave="" bgcolor="#ffcc33">                     <div align="right">&gt;=</div>
132   </td>
133    <td nosave="" bgcolor="#66ffff">5</td>
134   </tr>
135               </tbody>         </table>
136       </ul>
137   </ul>
138    <h2> <font size="+1">The __next__ method</font></h2>
139   Extension types wishing to implement the iterator interface should define
140  a method called <b><tt>__next__</tt></b>, <i>not</i> <tt>next</tt>. The Python
141  system will automatically supply a <tt>next</tt> method which calls your
142 <span style="font-family: monospace;">__next__</span>.  <b>Do NOT explicitly give your type a <tt>next</tt> method</b>,
143 or bad things could happen (see note 3).  
144 <h2> <font size="+1">Special Method Table</font></h2>
145   This table lists all of the special methods together with their parameter
146  and return types. A parameter named <b>self</b> is of the type the method
147  belongs to. Other untyped parameters are generic Python objects.  
148 <p>You don't have to declare your method as taking these parameter types.
149  If you declare different types, conversions will be performed as necessary.
150  <br>
151  &nbsp; <table nosave="" bgcolor="#ccffff" border="1" cellpadding="5" cellspacing="0">
152   <tbody>
153      <tr nosave="" bgcolor="#ffcc33">
154   <td nosave=""><b>Name</b></td>
155    <td><b>Parameters</b></td>
156    <td><b>Return type</b></td>
157    <td><b>Description</b></td>
158   </tr>
159    <tr nosave="" bgcolor="#66ffff">
160   <td colspan="4" nosave=""><b>General</b></td>
161   </tr>
162    <tr>
163   <td><tt>__new__</tt></td>
164    <td>self, ...</td>
165    <td>&nbsp;</td>
166    <td>Basic initialisation (no direct Python equivalent)</td>
167   </tr>
168    <tr>
169   <td><tt>__init__</tt></td>
170    <td>self, ...</td>
171    <td>&nbsp;</td>
172    <td>Further initialisation</td>
173   </tr>
174    <tr>
175   <td><tt>__dealloc__</tt></td>
176    <td>self</td>
177    <td>&nbsp;</td>
178    <td>Basic deallocation (no direct Python equivalent)</td>
179   </tr>
180    <tr>
181   <td><tt>__cmp__</tt></td>
182    <td>x, y</td>
183    <td>int</td>
184    <td>3-way comparison</td>
185   </tr>
186    <tr>
187   <td><tt>__richcmp__</tt></td>
188    <td>x, y, int op</td>
189    <td>object</td>
190    <td>Rich comparison (no direct Python equivalent)</td>
191   </tr>
192    <tr>
193   <td><tt>__str__</tt></td>
194    <td>self</td>
195    <td>object</td>
196    <td>str(self)</td>
197   </tr>
198    <tr>
199   <td><tt>__repr__</tt></td>
200    <td>self</td>
201    <td>object</td>
202    <td>repr(self)</td>
203   </tr>
204    <tr nosave="">
205   <td nosave=""><tt>__hash__</tt></td>
206    <td>self</td>
207    <td>int</td>
208    <td>Hash function</td>
209   </tr>
210    <tr>
211   <td><tt>__call__</tt></td>
212    <td>self, ...</td>
213    <td>object</td>
214    <td>self(...)</td>
215   </tr>
216    <tr>
217   <td><tt>__iter__</tt></td>
218    <td>self</td>
219    <td>object</td>
220    <td>Return iterator for sequence</td>
221   </tr>
222    <tr>
223   <td><tt>__getattr__</tt></td>
224    <td>self, name</td>
225    <td>object</td>
226    <td>Get attribute</td>
227   </tr>
228    <tr>
229   <td><tt>__setattr__</tt></td>
230    <td>self, name, val</td>
231    <td>&nbsp;</td>
232    <td>Set attribute</td>
233   </tr>
234    <tr>
235   <td><tt>__delattr__</tt></td>
236    <td>self, name</td>
237    <td>&nbsp;</td>
238    <td>Delete attribute</td>
239   </tr>
240    <tr nosave="" bgcolor="#66ffff">
241   <td colspan="4" nosave=""><b>Arithmetic operators</b></td>
242   </tr>
243    <tr>
244   <td><tt>__add__</tt></td>
245    <td>x, y</td>
246    <td>object</td>
247    <td>binary + operator</td>
248   </tr>
249    <tr>
250   <td><tt>__sub__</tt></td>
251    <td>x, y</td>
252    <td>object</td>
253    <td>binary - operator</td>
254   </tr>
255    <tr>
256   <td><tt>__mul__</tt></td>
257    <td>x, y</td>
258    <td>object</td>
259    <td>* operator</td>
260   </tr>
261    <tr>
262   <td><tt>__div__</tt></td>
263    <td>x, y</td>
264    <td>object</td>
265    <td>/&nbsp; operator for old-style division</td>
266   </tr>
267    <tr>
268   <td><tt>__floordiv__</tt></td>
269    <td>x, y</td>
270    <td>object</td>
271    <td>//&nbsp; operator</td>
272   </tr>
273    <tr>
274   <td><tt>__truediv__</tt></td>
275    <td>x, y</td>
276    <td>object</td>
277    <td>/&nbsp; operator for new-style division</td>
278   </tr>
279    <tr>
280   <td><tt>__mod__</tt></td>
281    <td>x, y</td>
282    <td>object</td>
283    <td>% operator</td>
284   </tr>
285    <tr>
286   <td><tt>__divmod__</tt></td>
287    <td>x, y</td>
288    <td>object</td>
289    <td>combined div and mod</td>
290   </tr>
291    <tr>
292   <td><tt>__pow__</tt></td>
293    <td>x, y, z</td>
294    <td>object</td>
295    <td>** operator or pow(x, y, z)</td>
296   </tr>
297    <tr>
298   <td><tt>__neg__</tt></td>
299    <td>self</td>
300    <td>object</td>
301    <td>unary - operator</td>
302   </tr>
303    <tr>
304   <td><tt>__pos__</tt></td>
305    <td>self</td>
306    <td>object</td>
307    <td>unary + operator</td>
308   </tr>
309    <tr>
310   <td><tt>__abs__</tt></td>
311    <td>self</td>
312    <td>object</td>
313    <td>absolute value</td>
314   </tr>
315    <tr>
316   <td><tt>__nonzero__</tt></td>
317    <td>self</td>
318    <td>int</td>
319    <td>convert to boolean</td>
320   </tr>
321    <tr>
322   <td><tt>__invert__</tt></td>
323    <td>self</td>
324    <td>object</td>
325    <td>~ operator</td>
326   </tr>
327    <tr>
328   <td><tt>__lshift__</tt></td>
329    <td>x, y</td>
330    <td>object</td>
331    <td>&lt;&lt; operator</td>
332   </tr>
333    <tr>
334   <td><tt>__rshift__</tt></td>
335    <td>x, y</td>
336    <td>object</td>
337    <td>&gt;&gt; operator</td>
338   </tr>
339    <tr>
340   <td><tt>__and__</tt></td>
341    <td>x, y</td>
342    <td>object</td>
343    <td>&amp; operator</td>
344   </tr>
345    <tr>
346   <td><tt>__or__</tt></td>
347    <td>x, y</td>
348    <td>object</td>
349    <td>| operator</td>
350   </tr>
351    <tr>
352   <td><tt>__xor__</tt></td>
353    <td>x, y</td>
354    <td>object</td>
355    <td>^ operator</td>
356   </tr>
357    <tr nosave="" bgcolor="#66ffff">
358   <td colspan="4" nosave=""><b>Numeric conversions</b></td>
359   </tr>
360    <tr>
361   <td><tt>__int__</tt></td>
362    <td>self</td>
363    <td>object</td>
364    <td>Convert to integer</td>
365   </tr>
366    <tr>
367   <td><tt>__long__</tt></td>
368    <td>self</td>
369    <td>object</td>
370    <td>Convert to long integer</td>
371   </tr>
372    <tr>
373   <td><tt>__float__</tt></td>
374    <td>self</td>
375    <td>object</td>
376    <td>Convert to float</td>
377   </tr>
378    <tr>
379   <td><tt>__oct__</tt></td>
380    <td>self</td>
381    <td>object</td>
382    <td>Convert to octal</td>
383   </tr>
384    <tr>
385   <td><tt>__hex__</tt></td>
386    <td>self</td>
387    <td>object</td>
388    <td>Convert to hexadecimal</td>
389   </tr>
390    <tr nosave="" bgcolor="#66ffff">
391   <td colspan="4" nosave=""><b>In-place arithmetic operators</b></td>
392   </tr>
393    <tr>
394   <td><tt>__iadd__</tt></td>
395    <td>self, x</td>
396    <td>object</td>
397    <td>+= operator</td>
398   </tr>
399    <tr>
400   <td><tt>__isub__</tt></td>
401    <td>self, x</td>
402    <td>object</td>
403    <td>-= operator</td>
404   </tr>
405    <tr>
406   <td><tt>__imul__</tt></td>
407    <td>self, x</td>
408    <td>object</td>
409    <td>*= operator</td>
410   </tr>
411    <tr>
412   <td><tt>__idiv__</tt></td>
413    <td>self, x</td>
414    <td>object</td>
415    <td>/= operator for old-style division</td>
416   </tr>
417    <tr>
418   <td><tt>__ifloordiv__</tt></td>
419    <td>self, x</td>
420    <td>object</td>
421    <td>//= operator</td>
422   </tr>
423    <tr>
424   <td><tt>__itruediv__</tt></td>
425    <td>self, x</td>
426    <td>object</td>
427    <td>/= operator for new-style division</td>
428   </tr>
429    <tr>
430   <td><tt>__imod__</tt></td>
431    <td>self, x</td>
432    <td>object</td>
433    <td>%= operator</td>
434   </tr>
435    <tr>
436   <td><tt>__ipow__</tt></td>
437    <td>x, y, z</td>
438    <td>object</td>
439    <td>**= operator</td>
440   </tr>
441    <tr>
442   <td><tt>__ilshift__</tt></td>
443    <td>self, x</td>
444    <td>object</td>
445    <td>&lt;&lt;= operator</td>
446   </tr>
447    <tr>
448   <td><tt>__irshift__</tt></td>
449    <td>self, x</td>
450    <td>object</td>
451    <td>&gt;&gt;= operator</td>
452   </tr>
453    <tr>
454   <td><tt>__iand__</tt></td>
455    <td>self, x</td>
456    <td>object</td>
457    <td>&amp;= operator</td>
458   </tr>
459    <tr>
460   <td><tt>__ior__</tt></td>
461    <td>self, x</td>
462    <td>object</td>
463    <td>|= operator</td>
464   </tr>
465    <tr>
466   <td><tt>__ixor__</tt></td>
467    <td>self, x</td>
468    <td>object</td>
469    <td>^= operator</td>
470   </tr>
471    <tr nosave="" bgcolor="#66ffff">
472   <td colspan="4" nosave=""><b>Sequences and mappings</b></td>
473   </tr>
474    <tr>
475   <td><tt>__len__</tt></td>
476    <td>self</td>
477    <td>int</td>
478    <td>len(self)</td>
479   </tr>
480    <tr>
481   <td><tt>__getitem__</tt></td>
482    <td>self, x</td>
483    <td>object</td>
484    <td>self[x]</td>
485   </tr>
486    <tr>
487   <td><tt>__setitem__</tt></td>
488    <td>self, x, y</td>
489    <td>&nbsp;</td>
490    <td>self[x] = y</td>
491   </tr>
492    <tr>
493   <td><tt>__delitem__</tt></td>
494    <td>self, x</td>
495    <td>&nbsp;</td>
496    <td>del self[x]</td>
497   </tr>
498    <tr>
499   <td><tt>__getslice__</tt></td>
500    <td>self, int i, int j</td>
501    <td>object</td>
502    <td>self[i:j]</td>
503   </tr>
504    <tr>
505   <td><tt>__setslice__</tt></td>
506    <td>self, int i, int j, x</td>
507    <td>&nbsp;</td>
508    <td>self[i:j] = x</td>
509   </tr>
510    <tr>
511   <td><tt>__delslice__</tt></td>
512    <td>self, int i, int j</td>
513    <td>&nbsp;</td>
514    <td>del self[i:j]</td>
515   </tr>
516    <tr>
517   <td><tt>__contains__</tt></td>
518    <td>self, x</td>
519    <td>int</td>
520    <td>x in self</td>
521   </tr>
522    <tr nosave="" bgcolor="#66ffff">
523   <td colspan="4" nosave=""><b>Iterators</b></td>
524   </tr>
525    <tr>
526   <td><tt>__next__</tt></td>
527    <td>self</td>
528    <td>object</td>
529    <td>Get next item (called <tt>next</tt> in Python)</td>
530   </tr>
531    <tr nosave="" bgcolor="#66ffff">
532   <td colspan="4" nosave=""><b>Buffer interface</b>&nbsp; (no Python equivalents
533  - see note 1)</td>
534   </tr>
535    <tr>
536   <td><tt>__getreadbuffer__</tt></td>
537    <td>self, int i, void **p</td>
538    <td>&nbsp;</td>
539    <td>&nbsp;</td>
540   </tr>
541    <tr>
542   <td><tt>__getwritebuffer__</tt></td>
543    <td>self, int i, void **p</td>
544    <td>&nbsp;</td>
545    <td>&nbsp;</td>
546   </tr>
547    <tr>
548   <td><tt>__getsegcount__</tt></td>
549    <td>self, int *p</td>
550    <td>&nbsp;</td>
551    <td>&nbsp;</td>
552   </tr>
553    <tr>
554   <td><tt>__getcharbuffer__</tt></td>
555    <td>self, int i, char **p</td>
556    <td>&nbsp;</td>
557    <td>&nbsp;</td>
558   </tr>
559    <tr nosave="" bgcolor="#66ffff">
560   <td colspan="4" nosave=""><b>Descriptor objects</b>&nbsp; (no Python equivalents
561  - see note 2)</td>
562   </tr>
563    <tr>
564   <td><tt>__get__</tt></td>
565    <td>self, instance, class</td>
566    <td>object</td>
567    <td>Get value of attribute</td>
568   </tr>
569    <tr>
570   <td><tt>__set__</tt></td>
571    <td>self, instance, value</td>
572    <td>&nbsp;</td>
573    <td>Set value of attribute</td>
574   </tr>
575    <tr>
576   <td style="font-family: monospace;">__delete__</td>
577    <td>self, instance</td>
578    <td>&nbsp;</td>
579    <td>Delete attribute</td>
580   </tr>
581       </tbody> </table>
582    </p>
583  <p>Note 1: The buffer interface is intended for use by C code and is not
584 directly accessible from Python. It is described in the <a href="http://www.python.org/doc/current/api/api.html">Python/C API Reference 
585 Manual</a> under sections <a href="http://www.python.org/doc/current/api/abstract-buffer.html">6.6</a>
586 and <a href="http://www.python.org/doc/current/api/buffer-structs.html">10.6</a>.
587  </p>
588  <p>Note 2: Descriptor objects are part of the support mechanism for new-style
589  Python classes. See the <a href="http://www.python.org/doc/2.2.1/whatsnew/sect-rellinks.html#SECTION000320000000000000000">discussion
590  of descriptors in the Python documentation</a>. See also <a href="http://www.python.org/peps/pep-0252.html">PEP 252, "Making Types Look 
591 More Like Classes"</a>, and <a href="http://www.python.org/peps/pep-0253.html">PEP 253, "Subtyping Built-In 
592 Types"</a>. </p>
593  <p>Note 3: If your type defines a <tt>__new__</tt> method, any method called
594  <tt>new</tt> that you define will be overwritten with the system-supplied
595  <tt>new</tt> at module import time. </p>
596  <br>
597  <br>
598 </body></html>