<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"ext.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"ext.html" title='Extending and Embedding the Python Interpreter'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"prev" href=
"dnt-basics.html" />
<link rel=
"parent" href=
"defining-new-types.html" />
<link rel=
"next" href=
"node26.html" />
<meta name='aesop' content='information'
/>
<title>2.2 Type Methods
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"2.1.3 Supporting cyclic garbage"
href=
"node24.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"2. Defining New Types"
href=
"defining-new-types.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"2.2.1 Finalization and De-allocation"
href=
"node26.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Extending and Embedding the Python Interpreter
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><img src='../icons/blank.png'
border='
0' height='
32' alt='' width='
32'
/></td>
<td class='online-navigation'
><img src='../icons/blank.png'
border='
0' height='
32' alt='' width='
32'
/></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"node24.html">2.1.3 Supporting cyclic garbage
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"defining-new-types.html">2. Defining New Types
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"node26.html">2.2.1 Finalization and De-allocation
</A>
<!--End of Navigation Panel-->
<H1><A NAME=
"SECTION004200000000000000000"></A><A NAME=
"dnt-type-methods"></A>
This section aims to give a quick fly-by on the various type methods
you can implement and what they do.
Here is the definition of
<tt class=
"ctype">PyTypeObject
</tt>, with some fields only
used in debug builds omitted:
<pre>typedef struct _typeobject {
char *tp_name; /* For printing, in format
"<module>.<name>" */
int tp_basicsize, tp_itemsize; /* For allocation */
/* Methods to implement standard operations */
/* Method suites for standard classes */
PyNumberMethods *tp_as_number;
PySequenceMethods *tp_as_sequence;
PyMappingMethods *tp_as_mapping;
/* More standard operations (here for binary compatibility) */
getattrofunc tp_getattro;
setattrofunc tp_setattro;
/* Functions to access object as input/output buffer */
PyBufferProcs *tp_as_buffer;
/* Flags to define presence of optional/expanded features */
char *tp_doc; /* Documentation string */
/* Assigned meaning in release
2.0 */
/* call function for all accessible objects */
traverseproc tp_traverse;
/* delete references to contained objects */
/* Assigned meaning in release
2.1 */
richcmpfunc tp_richcompare;
/* weak reference enabler */
/* Added in release
2.2 */
iternextfunc tp_iternext;
/* Attribute descriptor and subclassing stuff */
struct PyMethodDef *tp_methods;
struct PyMemberDef *tp_members;
struct PyGetSetDef *tp_getset;
struct _typeobject *tp_base;
descrgetfunc tp_descr_get;
descrsetfunc tp_descr_set;
freefunc tp_free; /* Low-level free-memory routine */
inquiry tp_is_gc; /* For PyObject_IS_GC */
PyObject *tp_mro; /* method resolution order */
<a href=
"typestruct.txt" type=
"text/plain">Download as text (original file name:
<span class=
"file">typestruct.h
</span>).
</a>
Now that's a
<em>lot
</em> of methods. Don't worry too much though - if
you have a type you want to define, the chances are very good that you
will only implement a handful of these.
As you probably expect by now, we're going to go over this and give
more information about the various handlers. We won't go in the order
they are defined in the structure, because there is a lot of
historical baggage that impacts the ordering of the fields; be sure
your type initialization keeps the fields in the right order! It's
often easiest to find an example that includes all the fields you need
(even if they're initialized to
<code>0</code>) and then change the values
<div class=
"verbatim"><pre>
char *tp_name; /* For printing */
The name of the type - as mentioned in the last section, this will
appear in various places, almost entirely for diagnostic purposes.
Try to choose something that will be helpful in such a situation!
<div class=
"verbatim"><pre>
int tp_basicsize, tp_itemsize; /* For allocation */
These fields tell the runtime how much memory to allocate when new
objects of this type are created. Python has some built-in support
for variable length structures (think: strings, lists) which is where
the
<tt class=
"member">tp_itemsize
</tt> field comes in. This will be dealt with
<div class=
"verbatim"><pre>
Here you can put a string (or its address) that you want returned when
the Python script references
<code>obj.__doc__
</code> to retrieve the
Now we come to the basic type methods--the ones most extension types
<p><br /></p><hr class='online-navigation'
/>
<div class='online-navigation'
>
<!--Table of Child-Links-->
<A NAME=
"CHILD_LINKS"><STRONG>Subsections
</STRONG></a>
<LI><A href=
"node26.html">2.2.1 Finalization and De-allocation
</a>
<LI><A href=
"node27.html">2.2.2 Object Presentation
</a>
<LI><A href=
"node28.html">2.2.3 Attribute Management
</a>
<LI><A href=
"node29.html">2.2.3.1 Generic Attribute Management
</a>
<LI><A href=
"node30.html">2.2.3.2 Type-specific Attribute Management
</a>
<LI><A href=
"node31.html">2.2.4 Object Comparison
</a>
<LI><A href=
"node32.html">2.2.5 Abstract Protocol Support
</a>
<LI><A href=
"node33.html">2.2.6 More Suggestions
</a>
<!--End of Table of Child-Links-->
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"2.1.3 Supporting cyclic garbage"
href=
"node24.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"2. Defining New Types"
href=
"defining-new-types.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"2.2.1 Finalization and De-allocation"
href=
"node26.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Extending and Embedding the Python Interpreter
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><img src='../icons/blank.png'
border='
0' height='
32' alt='' width='
32'
/></td>
<td class='online-navigation'
><img src='../icons/blank.png'
border='
0' height='
32' alt='' width='
32'
/></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"node24.html">2.1.3 Supporting cyclic garbage
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"defining-new-types.html">2. Defining New Types
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"node26.html">2.2.1 Finalization and De-allocation
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.