Linux/Unix > Python Essential Reference, Second Edition > B. Extending and Embedding Python > Special Methods for Types | See All Titles

< BACK | Make Note | Bookmark | CONTINUE >

155117184014003188065099048180054212144238241179195140058238111161104058145062051086240154

### Special Methods for Types

This section describes the C data structures used to implement types. This is an advanced topic and the presentation here is only intended to be a quick reference.

Types are defined by a structure with the following fields. Many of the fields in this structures use `typedef` names such as `hashfunc`, `binaryfunc`, `intargfunc`, and so forth. These types are defined later in this appendix in Table B.26.

    typedef struct _typeobject {

        PyObject_VAR_HEAD
        char              *tp_name;
        int                tp_basicsize;
        int                tp_itemsize;
        destructor         tp_dealloc;
        printfunc          tp_print;
        getattrfunc        tp_getattr;
        setattrfunc        tp_setattr;
        cmpfunc            tp_compare;
        reprfunc           tp_repr;

        PyNumberMethods   *tp_as_number;
        PySequenceMethods *tp_as_sequence;
        PyMappingMethods  *tp_as_mapping;

        hashfunc           tp_hash;
        ternaryfunc        tp_call;
        reprfunc           tp_str;
        getattrofunc       tp_getattro;
        setattrofunc       tp_setattro;

        PyBufferProcs     *tp_as_buffer;
        long               tp_flags;
        traverseproc       tp_traverse;
        inquiry            tp_clear;
        richcmpfunc        tp_richcompare;
        long               tp_weaklistoffset;
    } PyTypeObject;

Most of the special methods for a type are encapsulated in four data structures: `PySequenceMethods`, `PyMappingMethods`, `PyNumberMethods`, and `PyBufferProcs`. Tables B.22 through B.26 show the contents of these structures.

##### Table B.22. `PySequenceMethods` Structure

C Datatype | Name | Python Method
--- | --- | ---
`(inquiry)` | `sq_length` | `_ _len_ _(``x` `)`
`(binaryfunc)` | `sq_concat` | `_ _add_ _(``x` `,``y` `)`
`(intargfunc)` | `sq_repeat` | `_ _mul_ _(``x` `,``n` `)`
`(intargfunc)` | `sq_item` | `_ _getitem_ _(``x` `,``n` `)`
`(intintargfunc)` | `sq_slice` | `_ _getslice_ _(``x` `,``i` `,``j` `)`
`(intobjargproc)` | `sq_ass_item` | `_ _setitem_ _(``x` `,``n` `,``v` `)`
`(intintobjargproc)` | `sq_ass_slice` | `_ _setslice_ _(``x` `,``i` `,``j` `,``v` `)`
`(objobjproc)` | `sq_contains` | `_ _contains_ _(``x` `,``o` `)`
`(binaryfunc)` | `sq_inplace_concat` | `_ _iadd_ _(``x` `,``y` `)`
`(intargfunc)` | `sq_inplace_repeat` | `_ _imul_ _(``x` `,``n` `)`

##### Table B.23. `PyMappingMethods` Structure

C Datatype | Name Python | Method
--- | --- | ---
`(inquiry)` | `mp_length` | `_ _len_ _(``x` `)`
`(binaryfunc)` | `mp_subscript` | `_ _getitem_ _(``x` `,``key` `)`
`(objobjargproc)` | `mp_ass_subscript` | `_ _setitem_ _(``x` `,``key` `,``value` `)`

##### Table B.24. `PyNumberMethods` Structure

C Datatype | Name | Python Method
--- | --- | ---
`(binaryfunc)` | `nb_add` | `_ _add_ _(``x` `,``y` `)`
`(binaryfunc)` | `nb_subtract` | `_ _sub_ _(``x` `,``y` `)`
`(binaryfunc)` | `nb_multiply` | `_ _mul_ _(``x` `,``y` `)`
`(binaryfunc)` | `nb_divide` | `_ _div_ _(``x` `,``y` `)`
`(binaryfunc)` | `nb_remainder` | `_ _mod_ _(``x` `,``y` `)`
`(binaryfunc)` | `nb_divmod` | `_ _divmod_ _(``x` `,``y` `)`
`(ternaryfunc)` | `nb_power` | `_ _pow_ _(``x` `,``y` `,``n` `)`
`(unaryfunc)` | `nb_negative` | `_ _neg_ _(``x` `)`
`(unaryfunc)` | `nb_positive` | `_ _pos_ _(``x` `)`
`(unaryfunc)` | `nb_absolute` | `_ _abs_ _(``x` `)`
`(inquiry)` | `nb_nonzero` | `_ _zero_ _(``x` `)`
`(unaryfunc)` | `nb_invert` | `_ _invert_ _(``x` `)`
`(binaryfunc)` | `nb_lshift` | `_ _lshift_ _(``x` `,``y` `)`
`(binaryfunc)` | `nb_rshift` | `_ _rshift_ _(``x` `,``y` `)`
`(binaryfunc)` | `nb_and` | `_ _and_ _(``x` `,``y` `)`
`(binaryfunc)` | `nb_xor` | `_ _xor_ _(``x` `,``y` `)`
`(binaryfunc)` | `nb_or` | `_ _or_ _(``x` `,``y` `)`
`(coercion)` | `nb_coerce` | `_ _coerce_ _(``x` `,``y` `)`
`(unaryfunc)` | `nb_int` | `_ _int_ _(``x` `)`
`(unaryfunc)` | `nb_long` | `_ _long_ _(``x` `)`
`(unaryfunc)` | `nb_float` | `_ _float_ _(``x` `)`
`(unaryfunc)` | `nb_oct` | `_ _oct_ _(``x` `)`
`(unaryfunc)` | `nb_hex` | `_ _hex_ _(``x` `)`
`(binaryfunc)` | `nb_inplace_add` | `_ _iadd_ _(``x` `,``y` `)`
`(binaryfunc)` | `nb_inplace_subtract` | `_ _isub_ _(``x` `,``y` `)`
`(binaryfunc)` | `nb_inplace_multiply` | `_ _imul_ _(``x` `,``y` `)`
`(binaryfunc)` | `nb_inplace_divide` | `_ _idiv_ _(``x` `,``y` `)`
`(binaryfunc)` | `nb_inplace_remainder` | `_ _imod_ _(``x` `,``y` `)`
`(ternaryfunc)` | `nb_inplace_power` | `_ _ipow_ _(``x` `,``y` `,``n` `)`
`(binaryfunc)` | `nb_inplace_lshift` | `_ _ilshift_ _(``x` `,``y` `)`
`(binaryfunc)` | `nb_inplace_rshift` | `_ _irshift_ _(``x` `,``y` `)`
`(binaryfunc)` | `nb_inplace_and` | `_ _iand_ _(``x` `,``y` `)`
`(binaryfunc)` | `nb_inplace_xor` | `_ _ixor_ _(``x` `,``y` `)`
`(binaryfunc)` | `nb_inplace_or` | `_ _ior_ _(``x` `,``y` `)`

##### Table B.25. `PyBufferProcs` Methods

C Datatype | Python Method
--- | ---
`(getreadbufferproc)` | `bf_getreadbuffer`
`(getwritebufferproc)` | `bf_getwritebuffer`
`(getsegcountproc)` | `bf_getsegcount`
`(getcharbufferproc)` | `bf_getcharbuffer`

##### Table B.26. C Prototypes for Methods Defined in This Section

C Datatype | Prototype
--- | ---
`(inquiry)` | `int (*)(PyObject *)`
`(unaryfunc)` | `PyObject (*)(PyObject *)`
`(binaryfunc)` | `PyObject (*)(PyObject *, PyObject *)`
`(ternaryfunc)` | `PyObject (*)(PyObject *, PyObject *, PyObject *)`
`(coercion)` | `int (*)(PyObject **, PyObject **)`
`(intargfunc)` | `PyObject (*)(PyObject *, int)`
`(intintargfunc)` | `PyObject (*)(PyObject *, int, int)`
`(intobjargproc)` | `int (*)(PyObject *, int, PyObject *)`
`(intintobjargproc)` | `int (*)(PyObject *, int, int, PyObject *)`
`(destructor)` | `void (*)(PyObject *)`
`(printfunc)` | `int (*)(PyObject *, FILE *, int)`
`(getattrfunc)` | `PyObject (*)(PyObject *, char *)`
`(getattrofunc)` | `PyObject (*)(PyObject *, PyObject *)`
`(setattrfunc)` | `int (*)(PyObject *, char *, PyObject *)`
`(setattrofunc)` | `int (*)(PyObject *, PyObject *, PyObject *)`
`(cmpfunc)` | `int (*)(PyObject *, PyObject *)`
`(reprfunc)` | `PyObject (*)(PyObject *)`
`(hashfunc)` | `long (*)(PyObject *)`
`(objobjfunc)` | `int (*)(PyObject *, PyObject *)`
`(getreadbufferproc)` | `int (*)(PyObject *, int, void **)`
`(getwritebufferproc)` | `int (*)(PyObject *, int, void **)`
`(getsegcountproc)` | `int (*)(PyObject *, int *)`
`(getcharbufferproc)` | `int (*)(PyObject *, int, const char **)`
`(richcmpfunc)` | `PyObject *(*)(PyObject *, PyObject *, int);`
`(visitproc)` | `int (*)(PyObject *, void *)`
`(traverseproc)` | `int (*)(PyObject *, visitproc, void *)`

| Last updated on 3/28/2002 Python Essential Reference, Second Edition, © 2002 New Riders Publishing

< BACK | Make Note | Bookmark | CONTINUE >

© 2002, O'Reilly & Associates, Inc.