2.2.6 More Suggestions

Remember that you can omit most of these functions, in which case you provide 0 as a value. There are type definitions for each of the functions you must provide. They are in object.h in the Python include directory that comes with the source distribution of Python.

In order to learn how to implement any specific method for your new datatype, do the following: Download and unpack the Python source distribution. Go the Objects directory, then search the C source files for tp_ plus the function you want (for example, tp_print or tp_compare). You will find examples of the function you want to implement.

When you need to verify that an object is an instance of the type you are implementing, use the PyObject_TypeCheck function. A sample of its use might be something like the following:

    if (! PyObject_TypeCheck(some_object, &MyType)) {
        PyErr_SetString(PyExc_TypeError, "arg #1 not a mything");
        return NULL;
    }
Vedete Circa questo documento... per informazioni su modifiche e suggerimenti.