<!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=
"next" href=
"building.html" />
<link rel=
"prev" href=
"intro.html" />
<link rel=
"parent" href=
"ext.html" />
<link rel=
"next" href=
"dnt-basics.html" />
<meta name='aesop' content='information'
/>
<title>2. Defining New Types
<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=
"1.12 Providing a C"
href=
"using-cobjects.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=
"Extending and Embedding the"
href=
"ext.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.1 The Basics"
href=
"dnt-basics.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=
"using-cobjects.html">1.12 Providing a C
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"ext.html">Extending and Embedding the
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"dnt-basics.html">2.1 The Basics
</A>
<!--End of Navigation Panel-->
<H1><A NAME=
"SECTION004000000000000000000"></A><A NAME=
"defining-new-types"></A>
As mentioned in the last chapter, Python allows the writer of an
extension module to define new types that can be manipulated from
Python code, much like strings and lists in core Python.
This is not hard; the code for all extension types follows a pattern,
but there are some details that you need to understand before you can
<div class=
"note"><b class=
"label">Note:
</b>
The way new types are defined changed dramatically (and for the
better) in Python
2.2. This document documents how to define new
types for Python
2.2 and later. If you need to support older
versions of Python, you will need to refer to older versions of this
<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=
"dnt-basics.html">2.1 The Basics
</a>
<LI><A href=
"node22.html">2.1.1 Adding data and methods to the Basic example
</a>
<LI><A href=
"node23.html">2.1.2 Providing finer control over data attributes
</a>
<LI><A href=
"node24.html">2.1.3 Supporting cyclic garbage collection
</a>
<LI><A href=
"dnt-type-methods.html">2.2 Type Methods
</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=
"1.12 Providing a C"
href=
"using-cobjects.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=
"Extending and Embedding the"
href=
"ext.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.1 The Basics"
href=
"dnt-basics.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=
"using-cobjects.html">1.12 Providing a C
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"ext.html">Extending and Embedding the
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"dnt-basics.html">2.1 The Basics
</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.