Py_INCREF(result); return result; } 

## 2.3.5 Abstract Protocol Support 

(continued from previous page) 

Python supports a variety of abstract ‘protocols;’ the specific interfaces provided to use these interfaces are docu- mented in abstract. 

A number of these abstract interfaces were defined early in the development of the Python implementation. In par- ticular, the number, mapping, and sequence protocols have been part of Python since the beginning. Other protocols have been added over time. For protocols which depend on several handler routines from the type implementation, the older protocols have been defined as optional blocks of handlers referenced by the type object. For newer pro- tocols there are additional slots in the main type object, with a flag bit being set to indicate that the slots are present and should be checked by the interpreter. (The flag bit does not indicate that the slot values are non- NULL . The flag may be set to indicate the presence of a slot, but a slot may still be unfilled.) 

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

If you wish your object to be able to act like a number, a sequence, or a mapping object, then you place the address of a structure that implements the C type$_{PyNumberMethods}$ ,$_{PySequenceMethods}$ , or$_{PyMappingMethods}$ , respectively. It is up to you to fill in this structure with appropriate values. You can find examples of the use of each of these in the$_{Objects}$directory of the Python source distribution. 

hashfunc tp_hash; 

This function, if you choose to provide it, should return a hash number for an instance of your data type. Here is a simple example: 

static Py_hash_t newdatatype_hash(newdatatypeobject *obj) { Py_hash_t result; result=obj->some_size +32767 *obj->some_number; if (result==-1 ) result=-2 ; return result; } 

$_{Py_hash_t}$is a signed integer type with a platform-varying width. Returning -1 from $_{tp_hash}$indicates an error, which is why you should be careful to avoid returning it when hash computation is successful, as seen above. 

ternaryfunc tp_call; 

This function is called when an instance of your data type is “called”, for example, if$_{obj1}$is an instance of your data type and the Python script contains$_{obj1('hello')}$ , the$_{tp_call}$ handler is invoked. 

This function takes three arguments: 

1. self is the instance of the data type which is the subject of the call. If the call is$_{obj1('hello')}$ , then self is$_{obj1}$ . 

2. args is a tuple containing the arguments to the call. You can use PyArg_ParseTuple() to extract the arguments. 

3. kwds is a dictionary of keyword arguments that were passed. If this is non-$_{NULL}$ and you support keyword arguments, use$_{PyArg_ParseTupleAndKeywords()}$ to extract the arguments. If you do not want to 

