<!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=
"node32.html" />
<link rel=
"prev" href=
"node28.html" />
<link rel=
"parent" href=
"dnt-type-methods.html" />
<link rel=
"next" href=
"node32.html" />
<meta name='aesop' content='information'
/>
<title>2.2.4 Object Comparison
</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.2.3.2 Type-specific Attribute Management"
href=
"node30.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.2 Type Methods"
href=
"dnt-type-methods.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.5 Abstract Protocol Support"
href=
"node32.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=
"node30.html">2.2.3.2 Type-specific Attribute Management
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"dnt-type-methods.html">2.2 Type Methods
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"node32.html">2.2.5 Abstract Protocol Support
</A>
<!--End of Navigation Panel-->
<H2><A NAME=
"SECTION004240000000000000000">
2.2.4 Object Comparison
</A>
<div class=
"verbatim"><pre>
The
<tt class=
"member">tp_compare
</tt> handler is called when comparisons are needed
and the object does not implement the specific rich comparison method
which matches the requested comparison. (It is always used if defined
and the
<tt class=
"cfunction">PyObject_Compare()
</tt> or
<tt class=
"cfunction">PyObject_Cmp()
</tt>
functions are used, or if
<tt class=
"function">cmp()
</tt> is used from Python.)
It is analogous to the
<tt class=
"method">__cmp__()
</tt> method. This function
should return
<code>-
1</code> if
<var>obj1
</var> is less than
<var>obj2
</var>,
<code>0</code> if they are equal, and
<code>1</code> if
<var>obj1
</var> is greater than
(It was previously allowed to return arbitrary negative or positive
integers for less than and greater than, respectively; as of Python
2.2, this is no longer allowed. In the future, other return values
may be assigned a different meaning.)
A
<tt class=
"member">tp_compare
</tt> handler may raise an exception. In this case it
should return a negative value. The caller has to test for the
exception using
<tt class=
"cfunction">PyErr_Occurred()
</tt>.
Here is a sample implementation:
<div class=
"verbatim"><pre>
newdatatype_compare(newdatatypeobject * obj1, newdatatypeobject * obj2)
if (obj1-
>obj_UnderlyingDatatypePtr-
>size
<
obj2-
>obj_UnderlyingDatatypePtr-
>size) {
else if (obj1-
>obj_UnderlyingDatatypePtr-
>size
>
obj2-
>obj_UnderlyingDatatypePtr-
>size) {
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"2.2.3.2 Type-specific Attribute Management"
href=
"node30.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.2 Type Methods"
href=
"dnt-type-methods.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.5 Abstract Protocol Support"
href=
"node32.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=
"node30.html">2.2.3.2 Type-specific Attribute Management
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"dnt-type-methods.html">2.2 Type Methods
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"node32.html">2.2.5 Abstract Protocol Support
</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.