| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="ext.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="ext.html" title='Extending and Embedding the Python Interpreter' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='last' href='about.html' title='About this document...' /> |
| 10 | <link rel='help' href='about.html' title='About this document...' /> |
| 11 | <link rel="next" href="node32.html" /> |
| 12 | <link rel="prev" href="node28.html" /> |
| 13 | <link rel="parent" href="dnt-type-methods.html" /> |
| 14 | <link rel="next" href="node32.html" /> |
| 15 | <meta name='aesop' content='information' /> |
| 16 | <title>2.2.4 Object Comparison</title> |
| 17 | </head> |
| 18 | <body> |
| 19 | <DIV CLASS="navigation"> |
| 20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 22 | <tr> |
| 23 | <td class='online-navigation'><a rel="prev" title="2.2.3.2 Type-specific Attribute Management" |
| 24 | href="node30.html"><img src='../icons/previous.png' |
| 25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 26 | <td class='online-navigation'><a rel="parent" title="2.2 Type Methods" |
| 27 | href="dnt-type-methods.html"><img src='../icons/up.png' |
| 28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 29 | <td class='online-navigation'><a rel="next" title="2.2.5 Abstract Protocol Support" |
| 30 | href="node32.html"><img src='../icons/next.png' |
| 31 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 32 | <td align="center" width="100%">Extending and Embedding the Python Interpreter</td> |
| 33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 34 | href="contents.html"><img src='../icons/contents.png' |
| 35 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 36 | <td class='online-navigation'><img src='../icons/blank.png' |
| 37 | border='0' height='32' alt='' width='32' /></td> |
| 38 | <td class='online-navigation'><img src='../icons/blank.png' |
| 39 | border='0' height='32' alt='' width='32' /></td> |
| 40 | </tr></table> |
| 41 | <div class='online-navigation'> |
| 42 | <b class="navlabel">Previous:</b> |
| 43 | <a class="sectref" rel="prev" href="node30.html">2.2.3.2 Type-specific Attribute Management</A> |
| 44 | <b class="navlabel">Up:</b> |
| 45 | <a class="sectref" rel="parent" href="dnt-type-methods.html">2.2 Type Methods</A> |
| 46 | <b class="navlabel">Next:</b> |
| 47 | <a class="sectref" rel="next" href="node32.html">2.2.5 Abstract Protocol Support</A> |
| 48 | </div> |
| 49 | <hr /></div> |
| 50 | </DIV> |
| 51 | <!--End of Navigation Panel--> |
| 52 | |
| 53 | <H2><A NAME="SECTION004240000000000000000"> |
| 54 | 2.2.4 Object Comparison</A> |
| 55 | </H2> |
| 56 | |
| 57 | <P> |
| 58 | <div class="verbatim"><pre> |
| 59 | cmpfunc tp_compare; |
| 60 | </pre></div> |
| 61 | |
| 62 | <P> |
| 63 | The <tt class="member">tp_compare</tt> handler is called when comparisons are needed |
| 64 | and the object does not implement the specific rich comparison method |
| 65 | which matches the requested comparison. (It is always used if defined |
| 66 | and the <tt class="cfunction">PyObject_Compare()</tt> or <tt class="cfunction">PyObject_Cmp()</tt> |
| 67 | functions are used, or if <tt class="function">cmp()</tt> is used from Python.) |
| 68 | It is analogous to the <tt class="method">__cmp__()</tt> method. This function |
| 69 | should return <code>-1</code> if <var>obj1</var> is less than |
| 70 | <var>obj2</var>, <code>0</code> if they are equal, and <code>1</code> if |
| 71 | <var>obj1</var> is greater than |
| 72 | <var>obj2</var>. |
| 73 | (It was previously allowed to return arbitrary negative or positive |
| 74 | integers for less than and greater than, respectively; as of Python |
| 75 | 2.2, this is no longer allowed. In the future, other return values |
| 76 | may be assigned a different meaning.) |
| 77 | |
| 78 | <P> |
| 79 | A <tt class="member">tp_compare</tt> handler may raise an exception. In this case it |
| 80 | should return a negative value. The caller has to test for the |
| 81 | exception using <tt class="cfunction">PyErr_Occurred()</tt>. |
| 82 | |
| 83 | <P> |
| 84 | Here is a sample implementation: |
| 85 | |
| 86 | <P> |
| 87 | <div class="verbatim"><pre> |
| 88 | static int |
| 89 | newdatatype_compare(newdatatypeobject * obj1, newdatatypeobject * obj2) |
| 90 | { |
| 91 | long result; |
| 92 | |
| 93 | if (obj1->obj_UnderlyingDatatypePtr->size < |
| 94 | obj2->obj_UnderlyingDatatypePtr->size) { |
| 95 | result = -1; |
| 96 | } |
| 97 | else if (obj1->obj_UnderlyingDatatypePtr->size > |
| 98 | obj2->obj_UnderlyingDatatypePtr->size) { |
| 99 | result = 1; |
| 100 | } |
| 101 | else { |
| 102 | result = 0; |
| 103 | } |
| 104 | return result; |
| 105 | } |
| 106 | </pre></div> |
| 107 | |
| 108 | <P> |
| 109 | |
| 110 | <DIV CLASS="navigation"> |
| 111 | <div class='online-navigation'> |
| 112 | <p></p><hr /> |
| 113 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 114 | <tr> |
| 115 | <td class='online-navigation'><a rel="prev" title="2.2.3.2 Type-specific Attribute Management" |
| 116 | href="node30.html"><img src='../icons/previous.png' |
| 117 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 118 | <td class='online-navigation'><a rel="parent" title="2.2 Type Methods" |
| 119 | href="dnt-type-methods.html"><img src='../icons/up.png' |
| 120 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 121 | <td class='online-navigation'><a rel="next" title="2.2.5 Abstract Protocol Support" |
| 122 | href="node32.html"><img src='../icons/next.png' |
| 123 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 124 | <td align="center" width="100%">Extending and Embedding the Python Interpreter</td> |
| 125 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 126 | href="contents.html"><img src='../icons/contents.png' |
| 127 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 128 | <td class='online-navigation'><img src='../icons/blank.png' |
| 129 | border='0' height='32' alt='' width='32' /></td> |
| 130 | <td class='online-navigation'><img src='../icons/blank.png' |
| 131 | border='0' height='32' alt='' width='32' /></td> |
| 132 | </tr></table> |
| 133 | <div class='online-navigation'> |
| 134 | <b class="navlabel">Previous:</b> |
| 135 | <a class="sectref" rel="prev" href="node30.html">2.2.3.2 Type-specific Attribute Management</A> |
| 136 | <b class="navlabel">Up:</b> |
| 137 | <a class="sectref" rel="parent" href="dnt-type-methods.html">2.2 Type Methods</A> |
| 138 | <b class="navlabel">Next:</b> |
| 139 | <a class="sectref" rel="next" href="node32.html">2.2.5 Abstract Protocol Support</A> |
| 140 | </div> |
| 141 | </div> |
| 142 | <hr /> |
| 143 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 144 | </DIV> |
| 145 | <!--End of Navigation Panel--> |
| 146 | <ADDRESS> |
| 147 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 148 | </ADDRESS> |
| 149 | </BODY> |
| 150 | </HTML> |