|
This page describes the special methods currently supported by Pyrex extension
types. A complete list of all the special methods appears in the table
at the bottom. Some of these methods behave differently from their Python
counterparts or have no direct Python counterparts, and require special
mention.
|
<
|
0 |
==
|
2 |
>
|
4 | ||
<=
|
1 |
!=
|
3 |
>=
|
5 |
You don't have to declare your method as taking these parameter types.
If you declare different types, conversions will be performed as necessary.
Name | Parameters | Return type | Description |
General | |||
__new__ | self, ... | Basic initialisation (no direct Python equivalent) | |
__init__ | self, ... | Further initialisation | |
__dealloc__ | self | Basic deallocation (no direct Python equivalent) | |
__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 |
__getattr__ | self, name | object | Get attribute |
__setattr__ | self, name, val | Set attribute | |
__delattr__ | self, name | Delete attribute | |
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 | |
__delitem__ | self, x | del self[x] | |
__getslice__ | self, int i, int j | object | self[i:j] |
__setslice__ | self, int i, int j, x | self[i:j] = x | |
__delslice__ | self, int i, int j | del self[i:j] | |
__contains__ | self, x | int | x in self |
Iterators | |||
__next__ | self | object | Get next item (called next in Python) |
Buffer interface (no Python equivalents - see note 1) | |||
__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 - see note 2) | |||
__get__ | self, x, y | object | |
__set__ | self, x, y | object |
Note 1: The buffer interface is intended for use by C code and is not directly accessible from Python. It is described in the Python/C API Reference Manual under sections 6.6 and 10.6.
Note 2: Descriptor objects are part of the support mechanism for new-style Python classes, and are not officially documented yet. The only information currently available is to be found in PEP 252, "Making Types Look More Like Classes", and PEP 253, "Subtyping Built-In Types".