set and dict comprehensions are supported
[cython.git] / docs / special_methods.rst
1 Special Methods of Extension Types
2 ===================================
3
4 This page describes the special methods currently supported by Cython extension
5 types. A complete list of all the special methods appears in the table at the
6 bottom. Some of these methods behave differently from their Python
7 counterparts or have no direct Python counterparts, and require special
8 mention.
9
10 .. Note: Everything said on this page applies only to extension types, defined
11     with the :keyword:`cdef class` statement. It doesn't apply to classes defined with the
12     Python :keyword:`class` statement, where the normal Python rules apply.  
13     
14 Declaration
15 ------------
16 Special methods of extension types must be declared with :keyword:`def`, not
17 :keyword:`cdef`.
18
19 Docstrings
20 -----------
21
22 Currently, docstrings are not fully supported in special methods of extension
23 types. You can place a docstring in the source to serve as a comment, but it
24 won't show up in the corresponding :attr:`__doc__` attribute at run time. (This is a
25 Python limitation -- there's nowhere in the `PyTypeObject` data structure to put
26 such docstrings.) 
27
28 Initialisation methods: :meth:`__cinit__` and :meth:`__init__`
29 ---------------------------------------------------------------
30 There are two methods concerned with initialising the object.
31
32 The :meth:`__cinit__` method is where you should perform basic C-level
33 initialisation of the object, including allocation of any C data structures
34 that your object will own. You need to be careful what you do in the
35 :meth:`__cinit__` method, because the object may not yet be a valid Python
36 object when it is called. Therefore, you must not invoke any Python operations
37 which might touch the object; in particular, do not try to call any of its
38 methods.
39
40 By the time your :meth:`__cinit__` method is called, memory has been allocated for the
41 object and any C attributes it has have been initialised to 0 or null. (Any
42 Python attributes have also been initialised to None, but you probably
43 shouldn't rely on that.) Your :meth:`__cinit__` method is guaranteed to be called
44 exactly once.
45
46 If your extension type has a base type, the :meth:`__cinit__` method of the base type
47 is automatically called before your :meth:`__cinit__` method is called; you cannot
48 explicitly call the inherited :meth:`__cinit__` method. If you need to pass a modified
49 argument list to the base type, you will have to do the relevant part of the
50 initialisation in the :meth:`__init__` method instead (where the normal rules for
51 calling inherited methods apply).
52
53 Any initialisation which cannot safely be done in the :meth:`__cinit__` method should
54 be done in the :meth:`__init__` method. By the time :meth:`__init__` is called, the object is
55 a fully valid Python object and all operations are safe. Under some
56 circumstances it is possible for :meth:`__init__` to be called more than once or not
57 to be called at all, so your other methods should be designed to be robust in
58 such situations.
59
60 Any arguments passed to the constructor will be passed to both the
61 :meth:`__cinit__` method and the :meth:`__init__` method. If you anticipate
62 subclassing your extension type in Python, you may find it useful to give the
63 :meth:`__cinit__` method `*` and `**` arguments so that it can accept and
64 ignore extra arguments. Otherwise, any Python subclass which has an
65 :meth:`__init__` with a different signature will have to override
66 :meth:`__new__` as well as :meth:`__init__`, which the writer of a Python
67 class wouldn't expect to have to do.  Finalization method: :meth:`__dealloc__`
68 The counterpart to the :meth:`__cinit__` method is the :meth:`__dealloc__`
69 method, which should perform the inverse of the :meth:`__cinit__` method. Any
70 C data structures that you allocated in your :meth:`__cinit__` method should
71 be freed in your :meth:`__dealloc__` method.
72
73 You need to be careful what you do in a :meth:`__dealloc__` method. By the time your
74 :meth:`__dealloc__` method is called, the object may already have been partially
75 destroyed and may not be in a valid state as far as Python is concerned, so
76 you should avoid invoking any Python operations which might touch the object.
77 In particular, don't call any other methods of the object or do anything which
78 might cause the object to be resurrected. It's best if you stick to just
79 deallocating C data.
80
81 You don't need to worry about deallocating Python attributes of your object,
82 because that will be done for you by Cython after your :meth:`__dealloc__` method
83 returns.
84
85 .. Note: There is no :meth:`__del__` method for extension types.
86
87 Arithmetic methods
88 -------------------
89
90 Arithmetic operator methods, such as :meth:`__add__`, behave differently from their
91 Python counterparts. There are no separate "reversed" versions of these
92 methods (:meth:`__radd__`, etc.) Instead, if the first operand cannot perform the
93 operation, the same method of the second operand is called, with the operands
94 in the same order.
95
96 This means that you can't rely on the first parameter of these methods being
97 "self", and you should test the types of both operands before deciding what to
98 do. If you can't handle the combination of types you've been given, you should
99 return `NotImplemented`.
100
101 This also applies to the in-place arithmetic method :meth:`__ipow__`. It doesn't apply
102 to any of the other in-place methods (:meth:`__iadd__`, etc.) which always
103 take `self` as the first argument.
104
105 Rich comparisons
106 -----------------
107
108 There are no separate methods for the individual rich comparison operations
109 (:meth:`__eq__`, :meth:`__le__`, etc.) Instead there is a single method
110 :meth:`__richcmp__` which takes an integer indicating which operation is to be
111 performed, as follows:
112              
113 +-----+-----+
114 |  <  |  0  |   
115 +-----+-----+
116 | ==  |  2  |
117 +-----+-----+
118 |  >  |  4  |
119 +-----+-----+
120 | <=  |  1  |   
121 +-----+-----+
122 | !=  |  3  |   
123 +-----+-----+
124 | >=  |  5  |
125 +-----+-----+
126
127 The :meth:`__next__` method
128 ----------------------------
129
130 Extension types wishing to implement the iterator interface should define a
131 method called :meth:`__next__`, not next. The Python system will automatically
132 supply a next method which calls your :meth:`__next__`. Do *NOT* explicitly
133 give your type a next method, or bad things could happen.
134
135 Type Testing in Special Methods 
136 --------------------------------
137
138 .. TODO document the Cython way using the overridden isinstance
139
140
141 Special Method Table
142 ---------------------
143
144 This table lists all of the special methods together with their parameter and
145 return types. In the table below, a parameter name of self is used to indicate
146 that the parameter has the type that the method belongs to. Other parameters
147 with no type specified in the table are generic Python objects.
148
149 You don't have to declare your method as taking these parameter types. If you
150 declare different types, conversions will be performed as necessary.
151  
152 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
153 | Name                  | Parameters                            | Return type |         Description                                 |
154 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
155 | General                                                                                                                           |
156 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
157 | __cinit__             |self, ...                              |             | Basic initialisation (no direct Python equivalent)  |
158 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
159 | __init__              |self, ...                              |             | Further initialisation                              |
160 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
161 | __dealloc__           |self                                   |             | Basic deallocation (no direct Python equivalent)    |
162 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
163 | __cmp__               |x, y                                   | int         | 3-way comparison                                    |
164 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
165 | __richcmp__           |x, y, int op                           | object      | Rich comparison (no direct Python equivalent)       |
166 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
167 | __str__               |self                                   | object      | str(self)                                           |
168 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
169 | __repr__              |self                                   | object      | repr(self)                                          |
170 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
171 | __hash__              |self                                   | int         | Hash function                                       |
172 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
173 | __call__              |self, ...                              | object      | self(...)                                           |
174 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
175 | __iter__              |self                                   | object      | Return iterator for sequence                        |
176 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
177 | __getattr__           |self, name                             | object      | Get attribute                                       |
178 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
179 | __setattr__           |self, name, val                        |             | Set attribute                                       |
180 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
181 | __delattr__           |self, name                             |             | Delete attribute                                    |
182 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
183 | Arithmetic operators                                                                                                              |
184 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
185 | __add__               | x, y                                  | object      | binary `+` operator                                 |
186 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
187 | __sub__               | x, y                                  | object      | binary `-` operator                                 |
188 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
189 | __mul__               | x, y                                  | object      | `*` operator                                        |
190 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
191 | __div__               | x, y                                  | object      | `/`  operator for old-style division                |
192 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
193 | __floordiv__          | x, y                                  | object      | `//`  operator                                      |
194 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
195 | __truediv__           | x, y                                  | object      | `/`  operator for new-style division                |
196 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
197 | __mod__               | x, y                                  | object      | `%` operator                                        |
198 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
199 | __divmod__            | x, y                                  | object      | combined div and mod                                |
200 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
201 | __pow__               | x, y, z                               | object      | `**` operator or pow(x, y, z)                       |
202 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
203 | __neg__               | self                                  | object      | unary `-` operator                                  |
204 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
205 | __pos__               | self                                  | object      | unary `+` operator                                  |
206 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
207 | __abs__               | self                                  | object      | absolute value                                      |
208 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
209 | __nonzero__           | self                                  | int         | convert to boolean                                  |
210 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
211 | __invert__            | self                                  | object      | `~` operator                                        |
212 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
213 | __lshift__            | x, y                                  | object      | `<<` operator                                       |
214 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
215 | __rshift__            | x, y                                  | object      | `>>` operator                                       |
216 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
217 | __and__               | x, y                                  | object      | `&` operator                                        |
218 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
219 | __or__                | x, y                                  | object      | `|` operator                                        |
220 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
221 | __xor__               | x, y                                  | object      | `^` operator                                        |
222 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
223 | Numeric conversions                                                                                                               |
224 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
225 | __int__               | self                                  | object      | Convert to integer                                  |
226 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
227 | __long__              | self                                  | object      | Convert to long integer                             |
228 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
229 | __float__             | self                                  | object      | Convert to float                                    |
230 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
231 | __oct__               | self                                  | object      | Convert to octal                                    |
232 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
233 | __hex__               | self                                  | object      | Convert to hexadecimal                              |
234 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
235 | __index__ (2.5+ only) | self                                  | object      | Convert to sequence index                           |
236 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
237 | In-place arithmetic operators                                                                                                     |
238 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
239 | __iadd__              | self, x                               | object      | `+=` operator                                       |
240 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
241 | __isub__              | self, x                               | object      | `-=` operator                                       |
242 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
243 | __imul__              | self, x                               | object      | `*=` operator                                       |
244 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
245 | __idiv__              | self, x                               | object      | `/=` operator for old-style division                |
246 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
247 | __ifloordiv__         | self, x                               | object      | `//=` operator                                      |
248 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
249 | __itruediv__          | self, x                               | object      | `/=` operator for new-style division                |
250 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
251 | __imod__              | self, x                               | object      | `%=` operator                                       |
252 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
253 | __ipow__              | x, y, z                               | object      | `**=` operator                                      |
254 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
255 | __ilshift__           | self, x                               | object      | `<<=` operator                                      |
256 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
257 | __irshift__           | self, x                               | object      | `>>=` operator                                      |
258 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
259 | __iand__              | self, x                               | object      | `&=` operator                                       |
260 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
261 | __ior__               | self, x                               | object      | `|=` operator                                       |
262 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
263 | __ixor__              | self, x                               | object      | `^=` operator                                       |
264 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
265 | Sequences and mappings                                                                                                            |
266 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
267 | __len__               | self  int                             |             | len(self)                                           |
268 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
269 | __getitem__           | self, x                               | object      | self[x]                                             |
270 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
271 | __setitem__           | self, x, y                            |             | self[x] = y                                         |
272 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
273 | __delitem__           | self, x                               |             | del self[x]                                         |
274 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
275 | __getslice__          | self, Py_ssize_t i, Py_ssize_t j      | object      | self[i:j]                                           |
276 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
277 | __setslice__          | self, Py_ssize_t i, Py_ssize_t j, x   |             | self[i:j] = x                                       |
278 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
279 | __delslice__          | self, Py_ssize_t i, Py_ssize_t j      |             | del self[i:j]                                       |
280 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
281 | __contains__          | self, x                               | int         | x in self                                           |
282 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
283 | Iterators                                                                                                                         |
284 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
285 | __next__              | self                                  | object      | Get next item (called next in Python)               |
286 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
287 | Buffer interface  (no Python equivalents - see note 1)                                                                            |
288 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
289 | __getreadbuffer__     | self, int i, void `**p`               |             |                                                     | 
290 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
291 | __getwritebuffer__    | self, int i, void `**p`               |             |                                                     |
292 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
293 | __getsegcount__       | self, int `*p`                        |             |                                                     |
294 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
295 | __getcharbuffer__     | self, int i, char `**p`               |             |                                                     |
296 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
297 | Descriptor objects  (see note 2)                                                                                                  |
298 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
299 | __get__               | self, instance, class                 | object      |         Get value of attribute                      |
300 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
301 | __set__               | self, instance, value                 |             |     Set value of attribute                          |
302 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
303 | __delete__            | self, instance                        |             |     Delete attribute                                |
304 +-----------------------+---------------------------------------+-------------+-----------------------------------------------------+
305
306 .. note:: (1) The buffer interface is intended for use by C code and is not directly
307         accessible from Python. It is described in the Python/C API Reference Manual
308         under sections 6.6 and 10.6.
309
310 .. note:: (2) Descriptor objects are part of the support mechanism for new-style
311         Python classes. See the discussion of descriptors in the Python documentation.
312         See also PEP 252, "Making Types Look More Like Classes", and PEP 253,
313         "Subtyping Built-In Types".
314