1 <!DOCTYPE doctype PUBLIC "-//w3c//dtd html 4.0 transitional//en">
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>
6 <h1> <hr width="100%">Special Methods of Extension Types
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
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>
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
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>
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>
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>
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:
103 <table nosave="" border="0" cellpadding="5" cellspacing="0">
106 <td nosave="" bgcolor="#ffcc33" width="30"> <div align="right"><</div>
108 <td nosave="" bgcolor="#66ffff" width="30">0</td>
111 <td nosave="" bgcolor="#ffcc33" width="30"> <div align="right">==</div>
113 <td nosave="" bgcolor="#66ffff" width="30">2</td>
116 <td nosave="" bgcolor="#ffcc33" width="30"> <div align="right">></div>
118 <td nosave="" bgcolor="#66ffff" width="30">4</td>
121 <td nosave="" bgcolor="#ffcc33"> <div align="right"><=</div>
123 <td nosave="" bgcolor="#66ffff">1</td>
126 <td nosave="" bgcolor="#ffcc33"> <div align="right">!=</div>
128 <td nosave="" bgcolor="#66ffff">3</td>
131 <td nosave="" bgcolor="#ffcc33"> <div align="right">>=</div>
133 <td nosave="" bgcolor="#66ffff">5</td>
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.
151 <table nosave="" bgcolor="#ccffff" border="1" cellpadding="5" cellspacing="0">
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>
159 <tr nosave="" bgcolor="#66ffff">
160 <td colspan="4" nosave=""><b>General</b></td>
163 <td><tt>__new__</tt></td>
166 <td>Basic initialisation (no direct Python equivalent)</td>
169 <td><tt>__init__</tt></td>
172 <td>Further initialisation</td>
175 <td><tt>__dealloc__</tt></td>
178 <td>Basic deallocation (no direct Python equivalent)</td>
181 <td><tt>__cmp__</tt></td>
184 <td>3-way comparison</td>
187 <td><tt>__richcmp__</tt></td>
188 <td>x, y, int op</td>
190 <td>Rich comparison (no direct Python equivalent)</td>
193 <td><tt>__str__</tt></td>
199 <td><tt>__repr__</tt></td>
205 <td nosave=""><tt>__hash__</tt></td>
208 <td>Hash function</td>
211 <td><tt>__call__</tt></td>
217 <td><tt>__iter__</tt></td>
220 <td>Return iterator for sequence</td>
223 <td><tt>__getattr__</tt></td>
226 <td>Get attribute</td>
229 <td><tt>__setattr__</tt></td>
230 <td>self, name, val</td>
232 <td>Set attribute</td>
235 <td><tt>__delattr__</tt></td>
238 <td>Delete attribute</td>
240 <tr nosave="" bgcolor="#66ffff">
241 <td colspan="4" nosave=""><b>Arithmetic operators</b></td>
244 <td><tt>__add__</tt></td>
247 <td>binary + operator</td>
250 <td><tt>__sub__</tt></td>
253 <td>binary - operator</td>
256 <td><tt>__mul__</tt></td>
262 <td><tt>__div__</tt></td>
265 <td>/ operator for old-style division</td>
268 <td><tt>__floordiv__</tt></td>
271 <td>// operator</td>
274 <td><tt>__truediv__</tt></td>
277 <td>/ operator for new-style division</td>
280 <td><tt>__mod__</tt></td>
286 <td><tt>__divmod__</tt></td>
289 <td>combined div and mod</td>
292 <td><tt>__pow__</tt></td>
295 <td>** operator or pow(x, y, z)</td>
298 <td><tt>__neg__</tt></td>
301 <td>unary - operator</td>
304 <td><tt>__pos__</tt></td>
307 <td>unary + operator</td>
310 <td><tt>__abs__</tt></td>
313 <td>absolute value</td>
316 <td><tt>__nonzero__</tt></td>
319 <td>convert to boolean</td>
322 <td><tt>__invert__</tt></td>
328 <td><tt>__lshift__</tt></td>
331 <td><< operator</td>
334 <td><tt>__rshift__</tt></td>
337 <td>>> operator</td>
340 <td><tt>__and__</tt></td>
343 <td>& operator</td>
346 <td><tt>__or__</tt></td>
352 <td><tt>__xor__</tt></td>
357 <tr nosave="" bgcolor="#66ffff">
358 <td colspan="4" nosave=""><b>Numeric conversions</b></td>
361 <td><tt>__int__</tt></td>
364 <td>Convert to integer</td>
367 <td><tt>__long__</tt></td>
370 <td>Convert to long integer</td>
373 <td><tt>__float__</tt></td>
376 <td>Convert to float</td>
379 <td><tt>__oct__</tt></td>
382 <td>Convert to octal</td>
385 <td><tt>__hex__</tt></td>
388 <td>Convert to hexadecimal</td>
390 <tr nosave="" bgcolor="#66ffff">
391 <td colspan="4" nosave=""><b>In-place arithmetic operators</b></td>
394 <td><tt>__iadd__</tt></td>
400 <td><tt>__isub__</tt></td>
406 <td><tt>__imul__</tt></td>
412 <td><tt>__idiv__</tt></td>
415 <td>/= operator for old-style division</td>
418 <td><tt>__ifloordiv__</tt></td>
421 <td>//= operator</td>
424 <td><tt>__itruediv__</tt></td>
427 <td>/= operator for new-style division</td>
430 <td><tt>__imod__</tt></td>
436 <td><tt>__ipow__</tt></td>
439 <td>**= operator</td>
442 <td><tt>__ilshift__</tt></td>
445 <td><<= operator</td>
448 <td><tt>__irshift__</tt></td>
451 <td>>>= operator</td>
454 <td><tt>__iand__</tt></td>
457 <td>&= operator</td>
460 <td><tt>__ior__</tt></td>
466 <td><tt>__ixor__</tt></td>
471 <tr nosave="" bgcolor="#66ffff">
472 <td colspan="4" nosave=""><b>Sequences and mappings</b></td>
475 <td><tt>__len__</tt></td>
481 <td><tt>__getitem__</tt></td>
487 <td><tt>__setitem__</tt></td>
493 <td><tt>__delitem__</tt></td>
499 <td><tt>__getslice__</tt></td>
500 <td>self, int i, int j</td>
505 <td><tt>__setslice__</tt></td>
506 <td>self, int i, int j, x</td>
508 <td>self[i:j] = x</td>
511 <td><tt>__delslice__</tt></td>
512 <td>self, int i, int j</td>
514 <td>del self[i:j]</td>
517 <td><tt>__contains__</tt></td>
522 <tr nosave="" bgcolor="#66ffff">
523 <td colspan="4" nosave=""><b>Iterators</b></td>
526 <td><tt>__next__</tt></td>
529 <td>Get next item (called <tt>next</tt> in Python)</td>
531 <tr nosave="" bgcolor="#66ffff">
532 <td colspan="4" nosave=""><b>Buffer interface</b> (no Python equivalents
536 <td><tt>__getreadbuffer__</tt></td>
537 <td>self, int i, void **p</td>
542 <td><tt>__getwritebuffer__</tt></td>
543 <td>self, int i, void **p</td>
548 <td><tt>__getsegcount__</tt></td>
549 <td>self, int *p</td>
554 <td><tt>__getcharbuffer__</tt></td>
555 <td>self, int i, char **p</td>
559 <tr nosave="" bgcolor="#66ffff">
560 <td colspan="4" nosave=""><b>Descriptor objects</b> (no Python equivalents
564 <td><tt>__get__</tt></td>
565 <td>self, instance, class</td>
567 <td>Get value of attribute</td>
570 <td><tt>__set__</tt></td>
571 <td>self, instance, value</td>
573 <td>Set value of attribute</td>
576 <td style="font-family: monospace;">__delete__</td>
577 <td>self, instance</td>
579 <td>Delete attribute</td>
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>.
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
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>