Special Methods of Extension Types

This page lists all of the special methods currently supported by Pyrex extension types, and indicates their parameter and return types. Note that you don't declare the parameters as having those types; the typing is implicit.

Some of these methods behave differently from their Python counterparts or have no direct Python counterparts, and require special mention.

Initialisation methods

There are two methods concerned with initialising the object, __new__ and __init__.

The __new__ method is where you should perform basic initialisation of the object to get it into a valid state. Unlike the corresponding method in Python, your __new__ method is not responsible for creating the object. By the time your __new__ method is called, memory has been allocated for the object, any C attributes it has have been initialised to 0 or null,  and any Python attributes  have been initialised to None. You should perform any other initialisation necessary, such as allocating memory for C data structures and giving initial values to C attributes. Your __new__ method is guaranteed to only be called once.

You can also optionally provide an __init__ method, which behaves the same way as its Python counterpart. It is called after the object has been created and initialised with __new__, and may perform any other initialisation you want. Unlike the __new__ method, it is possible for the __init__ method to be called more than once. Also, provided your __new__ method has done its job properly, the object will be in a valid state when your __init__ method is called.

Deallocation method

Pyrex extension types do not have a __del__ method. Instead, they have a __dealloc__ method, which should perform the inverse of the __new__ method. Any C data structures that you allocated in your __new__ method should be freed in your __dealloc__ method.

You need to be careful what you do in a __dealloc__ method. By the time your __dealloc__ method is called, the object may already have been partially destroyed and may not be in a valid state as far as Python is concerned, so you should avoid invoking any Python operations which might touch the object. In particular, don't call any other methods of the object or do anything which might cause the object to be resurrected. It's best if you stick to just deallocating C data.

You don't need to worry about deallocating Python attributes of your object, because that will be done for you by Pyrex after your __dealloc__ method returns.

Arithmetic methods

Rich comparisons

Unimplemented methods

There are currently no equivalents of __getattr__ and __setattr__. This may change in a later version.

Special Method Table

In the following table, a parameter named self is of the type the method belongs to. Other untyped parameters are generic Python objects.
 
Name Parameters Return type Description
General
__new__ self, ...   Basic initialisation (no direct Python equivalent)
__dealloc__ self   Basic deallocation (no direct Python equivalent)
__init__ self, ...   Further initialisation
__cmp__ x, y int 3-way comparison
__richcmp__ x, y, int op object Rich comparison (no direct Python equivalent)
__str__ self object str(self)
__repr__ self object repr(self)
__hash__ self int Hash function
__call__ self, ... object self(...)
__iter__ self object Return iterator for sequence
Arithmetic operators
__add__ x, y object binary + operator
__sub__ x, y object binary - operator
__mul__ x, y object * operator
__div__ x, y object /  operator for old-style division
__floordiv__ x, y object //  operator
__truediv__ x, y object /  operator for new-style division
__mod__ x, y object % operator
__divmod__ x, y object combined div and mod
__pow__ x, y, z object ** operator or pow(x, y, z)
__neg__ self object unary - operator
__pos__ self object unary + operator
__abs__ self object absolute value
__nonzero__ self int convert to boolean
__invert__ self object ~ operator
__lshift__ x, y object << operator
__rshift__ x, y object >> operator
__and__ x, y object & operator
__or__ x, y object | operator
__xor__ x, y object ^ operator
Numeric conversions
__int__ self object Convert to integer
__long__ self object Convert to long integer
__float__ self object Convert to float
__oct__ self object Convert to octal
__hex__ self object Convert to hexadecimal
In-place arithmetic operators
__iadd__ self, x object += operator
__isub__ self, x object -= operator
__imul__ self, x object *= operator
__idiv__ self, x object /= operator for old-style division
__ifloordiv__ self, x object //= operator
__itruediv__ self, x object /= operator for new-style division
__imod__ self, x object %= operator
__ipow__ x, y, z object **= operator
__ilshift__ self, x object <<= operator
__irshift__ self, x object >>= operator
__iand__ self, x object &= operator
__ior__ self, x object |= operator
__ixor__ self, x object ^= operator
Sequences and mappings
__len__ self int len(self)
__getitem__ self, x object self[x]
__setitem__ self, x, y   self[x] = y
__getslice__ self, int i, int j object self[i:j]
__setslice__ self, int i, int j, x   self[i:j] = x
__contains__ self, x int x in self
Iterators
__next__ self object Get next item (called next in Python)
Buffer interface  (no Python equivalents)
__getreadbuffer__ self, int i, void **p    
__getwritebuffer__ self, int i, void **p    
__getsegcount__ self, int *p    
__getcharbuffer__ self, int i, char **p    
Descriptor objects  (no Python equivalents)
__get__ self, x, y object  
__set__ self, x, y object