{"id":178589,"date":"2024-05-08T19:53:56","date_gmt":"2024-05-08T11:53:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/178589.html"},"modified":"2024-05-08T19:54:05","modified_gmt":"2024-05-08T11:54:05","slug":"python3%e4%b8%ad%e4%b8%ba%e4%bb%80%e4%b9%88%e5%8f%96%e6%b6%88%e4%ba%86-__cmp__-%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/178589.html","title":{"rendered":"python3\u4e2d\u4e3a\u4ec0\u4e48\u53d6\u6d88\u4e86 __cmp__ \u65b9\u6cd5"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27063051\/363e7613-6387-4641-8dae-2f83c7ab15f5.webp\" alt=\"python3\u4e2d\u4e3a\u4ec0\u4e48\u53d6\u6d88\u4e86 __cmp__ \u65b9\u6cd5\" \/><\/p>\n<p><p>\u5728<strong>Python 3<\/strong>\u4e2d\uff0c\u53d6\u6d88\u4e86<code>__cmp__<\/code>\u65b9\u6cd5\uff0c\u4e3b\u8981\u662f\u4e3a\u4e86\u7b80\u5316\u5bf9\u8c61\u95f4\u6bd4\u8f83\u7684\u5b9e\u73b0\u5e76\u63d0\u9ad8\u5176\u7075\u6d3b\u6027\u548c\u6548\u7387\u3002\u5177\u4f53\u6765\u8bf4\uff0cPython 3\u5f15\u5165\u4e86\u4e00\u5957\u66f4\u52a0\u7075\u6d3b\u7684\u7279\u6b8a\u65b9\u6cd5\u6765\u5206\u522b\u5904\u7406\u5927\u4e8e\u3001\u5c0f\u4e8e\u3001\u7b49\u4e8e\u7b49\u6bd4\u8f83\u64cd\u4f5c\uff0c\u5982<code>__lt__<\/code>\uff08\u5c0f\u4e8e\uff09\u3001<code>__le__<\/code>\uff08\u5c0f\u4e8e\u7b49\u4e8e\uff09\u3001<code>__eq__<\/code>\uff08\u7b49\u4e8e\uff09\u3001<code>__ne__<\/code>\uff08\u4e0d\u7b49\u4e8e\uff09\u3001<code>__gt__<\/code>\uff08\u5927\u4e8e\uff09\u548c<code>__ge__<\/code>\uff08\u5927\u4e8e\u7b49\u4e8e\uff09\u3002\u8fd9\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u660e\u786e\u4e14\u6613\u4e8e\u7406\u89e3\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u5bf9\u8c61\u4e4b\u95f4\u7684\u6bd4\u8f83\u884c\u4e3a\uff0c\u800c\u4e0d\u662f\u4f9d\u8d56\u4e8e\u5355\u4e00\u7684<code>__cmp__<\/code>\u65b9\u6cd5\u6765\u51b3\u5b9a\u6240\u6709\u6bd4\u8f83\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u8fd9\u4e9b\u65b0\u7684\u6bd4\u8f83\u7279\u6b8a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4ec5\u5b9e\u73b0\u6240\u9700\u7684\u6bd4\u8f83\u64cd\u4f5c\uff0c\u800c\u4e0d\u5fc5\u4e00\u6b21\u6027\u5904\u7406\u6240\u6709\u53ef\u80fd\u7684\u6bd4\u8f83\u60c5\u51b5\u3002\u800c\u5728Python 2\u4e2d\uff0c<code>__cmp__<\/code>\u9700\u8981\u6839\u636e\u6bd4\u8f83\u7684\u7ed3\u679c\u8fd4\u56de-1\u30010\u62161\u8fd9\u4e09\u4e2a\u4e0d\u540c\u7684\u503c\uff0c\u8fd9\u4f7f\u5f97\u6bd4\u8f83\u903b\u8f91\u7684\u5b9e\u73b0\u53d8\u5f97\u76f8\u5bf9\u590d\u6742\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u53d8\u5316\u539f\u56e0<\/h3>\n<\/p>\n<p><h4>\u7b80\u5316\u6bd4\u8f83\u673a\u5236<\/h4>\n<\/p>\n<p><p>\u53d6\u6d88<code>__cmp__<\/code>\u65b9\u6cd5\u662f\u4e3a\u4e86\u7b80\u5316\u5bf9\u8c61\u95f4\u7684\u6bd4\u8f83\u64cd\u4f5c\u3002\u5728\u4ee5\u524d\u7684Python\u7248\u672c\u4e2d\uff0c<code>__cmp__<\/code>\u9700\u8981\u7a0b\u5e8f\u5458\u7f16\u5199\u4e00\u4e2a\u590d\u6742\u7684\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u80fd\u591f\u8986\u76d6\u6240\u6709\u7684\u6bd4\u8f83\u60c5\u51b5\u3002\u8fd9\u4e0d\u4ec5\u589e\u52a0\u4e86\u5b9e\u73b0\u4e0a\u7684\u96be\u5ea6\uff0c\u800c\u4e14\u4f7f\u5f97\u9605\u8bfb\u548c\u7406\u89e3\u4ee3\u7801\u66f4\u52a0\u56f0\u96be\u3002Python 3\u7684\u6539\u53d8\uff0c\u8ba9\u6bd4\u8f83\u66f4\u52a0\u76f4\u89c2\u548c\u5177\u4f53\u3002<\/p>\n<\/p>\n<p><h4>\u63d0\u4f9b\u5b9a\u5236\u6027<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5177\u4f53\u7684\u6bd4\u8f83\u65b9\u6cd5\uff08\u5982<code>__lt__<\/code>\u3001<code>__le__<\/code>\u7b49\uff09\uff0c\u53ef\u4ee5\u7cbe\u786e\u63a7\u5236\u548c\u5b9a\u4e49\u5bf9\u8c61\u4e4b\u95f4\u7279\u5b9a\u6bd4\u8f83\u64cd\u4f5c\u7684\u884c\u4e3a\u3002\u8fd9\u79cd\u65b9\u5f0f\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7684\u5b9a\u5236\u6027\uff0c\u6709\u52a9\u4e8e\u521b\u5efa\u66f4\u52a0\u7b26\u5408\u903b\u8f91\u7684\u7c7b\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5982\u4f55\u5728Python 3\u4e2d\u8fdb\u884c\u6bd4\u8f83<\/h3>\n<\/p>\n<p><p>\u867d\u7136<code>__cmp__<\/code>\u88ab\u79fb\u9664\u4e86\uff0c\u4f46Python 3\u4e2d\u5bf9\u4e8e\u5b9a\u5236\u7c7b\u6765\u8bf4\uff0c\u4ecd\u7136\u53ef\u4ee5\u5b9e\u73b0\u5404\u79cd\u6bd4\u8f83\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>\u81ea\u5b9a\u4e49\u6bd4\u8f83\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>\u5728\u81ea\u5b9a\u4e49\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u6216\u591a\u4e2a\u6bd4\u8f83\u9b54\u672f\u65b9\u6cd5\uff0c\u5982<code>__lt__<\/code>\u548c<code>__eq__<\/code>\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5e94\u8be5\u8fd4\u56de\u5e03\u5c14\u503c\uff08True\u6216False\uff09\uff0c\u8868\u793a\u662f\u5426\u6ee1\u8db3\u76f8\u5e94\u7684\u6bd4\u8f83\u6761\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528functools\u6a21\u5757<\/h4>\n<\/p>\n<p><p>Python 3\u7684<code>functools<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>total_ordering<\/code>\u88c5\u9970\u5668\uff0c\u8fd9\u4e2a\u88c5\u9970\u5668\u53ef\u4ee5\u7528\u6765\u7b80\u5316\u5b8c\u6574\u6bd4\u8f83\u65b9\u6cd5\u7684\u5b9e\u73b0\u3002\u53ea\u9700\u8981\u5b9a\u4e49<code>__eq__<\/code>\u4ee5\u53ca<code>__lt__<\/code>\u3001<code>__le__<\/code>\u3001<code>__gt__<\/code>\u3001<code>__ge__<\/code>\u4e2d\u7684\u4e00\u4e2a\uff0c<code>total_ordering<\/code>\u88c5\u9970\u5668\u5c06\u81ea\u52a8\u6dfb\u52a0\u7f3a\u5931\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u6bd4\u8f83\u65b9\u6cd5\u7684\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u73b0\u5b9a\u5236\u7c7b\u7684\u6bd4\u8f83\u903b\u8f91\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\u4ee5\u7b26\u5408Python\u7684\u6bd4\u8f83\u534f\u8bae\uff1a<\/p>\n<\/p>\n<p><h4>\u8fd4\u56deNotImplemented<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6bd4\u8f83\u64cd\u4f5c\u4e0d\u9002\u7528\u4e8e\u67d0\u4e9b\u60c5\u51b5\uff0c\u5e94\u5f53\u8fd4\u56de<code>NotImplemented<\/code>\uff0c\u800c\u4e0d\u662f\u629b\u51fa\u5f02\u5e38\u3002\u8fd9\u5141\u8bb8\u89e3\u91ca\u5668\u5c1d\u8bd5\u5176\u4ed6\u6bd4\u8f83\u673a\u5236\u3002<\/p>\n<\/p>\n<p><h4>\u786e\u4fdd\u4e00\u81f4\u6027<\/h4>\n<\/p>\n<p><p>\u6bd4\u8f83\u65b9\u6cd5\u5e94\u8be5\u4fdd\u8bc1\u4e00\u81f4\u6027\u548c\u53cd\u5c04\u6027\u3002\u5373\uff0c\u5982\u679c<code>a &gt; b<\/code>\u4e3a\u771f\uff0c\u5219<code>b &lt; a<\/code>\u4e5f\u5e94\u4e3a\u771f\uff1b\u5982\u679c<code>a == b<\/code>\u4e3a\u771f\uff0c\u5219<code>b == a<\/code>\u4e5f\u5e94\u4e3a\u771f\u3002<\/p>\n<\/p>\n<p><h4>\u8003\u8651\u7c7b\u578b\u517c\u5bb9\u6027<\/h4>\n<\/p>\n<p><p>\u5728\u6bd4\u8f83\u4e0d\u540c\u7c7b\u578b\u7684\u5bf9\u8c61\u65f6\uff0c\u9700\u8981\u8c28\u614e\u5904\u7406\u3002\u4e00\u79cd\u597d\u7684\u5b9e\u8df5\u662f\uff0c\u5982\u679c\u88ab\u6bd4\u8f83\u7684\u5bf9\u8c61\u4e0d\u662f\u671f\u671b\u7684\u7c7b\u578b\uff0c\u8fd4\u56de<code>NotImplemented<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Python 3\u4e2d\u7684\u81ea\u5b9a\u4e49\u6bd4\u8f83\u65b9\u6cd5\u5b9e\u73b0\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from functools import total_ordering<\/p>\n<p>@total_ordering<\/p>\n<p>class MyNumber:<\/p>\n<p>    def __init__(self, value):<\/p>\n<p>        self.value = value<\/p>\n<p>    def __eq__(self, other):<\/p>\n<p>        if isinstance(other, MyNumber):<\/p>\n<p>            return self.value == other.value<\/p>\n<p>        return NotImplemented<\/p>\n<p>    def __lt__(self, other):<\/p>\n<p>        if isinstance(other, MyNumber):<\/p>\n<p>            return self.value &lt; other.value<\/p>\n<p>        return NotImplemented<\/p>\n<h2><strong>\u793a\u4f8b\u4f7f\u7528<\/strong><\/h2>\n<p>a = MyNumber(5)<\/p>\n<p>b = MyNumber(10)<\/p>\n<p>print(a &lt; b)  # \u8f93\u51fa: True<\/p>\n<p>print(a == b)  # \u8f93\u51fa: False<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c<strong>Python 3\u53d6\u6d88<code>__cmp__<\/code>\u65b9\u6cd5\u7684\u76ee\u7684\uff0c\u5728\u4e8e\u63d0\u4f9b\u4e00\u79cd\u66f4\u52a0\u660e\u6670\u548c\u7075\u6d3b\u7684\u65b9\u5f0f\u6765\u5904\u7406\u5bf9\u8c61\u95f4\u7684\u6bd4\u8f83<\/strong>\u3002\u901a\u8fc7\u5206\u5f00\u7684\u9b54\u672f\u65b9\u6cd5\uff0c\u7f16\u7a0b\u4eba\u5458\u53ef\u4ee5\u66f4\u7cbe\u786e\u5730\u5b9a\u4e49\u7c7b\u7684\u884c\u4e3a\uff0c\u4ece\u800c\u4f7f\u4ee3\u7801\u66f4\u52a0\u6613\u4e8e\u7ef4\u62a4\u548c\u7406\u89e3\u3002\u540c\u65f6\uff0c<code>functools.total_ordering<\/code>\u63d0\u4f9b\u8fdb\u4e00\u6b65\u7684\u4fbf\u5229\u548c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u4e3a\u4ec0\u4e48\u5728Python3\u4e2d\u53d6\u6d88\u4e86 <strong>cmp<\/strong> \u65b9\u6cd5?<\/strong><\/p>\n<ol>\n<li>\n<p><strong>\u4e3a\u4ec0\u4e48Python3\u79fb\u9664\u4e86 <strong>cmp<\/strong> \u65b9\u6cd5?<\/strong><br \/>\nPython3\u79fb\u9664\u4e86 <strong>cmp<\/strong> \u65b9\u6cd5\u4e3b\u8981\u662f\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u4e00\u81f4\u6027\u3002\u5728Python2\u4e2d\uff0c<strong>cmp<\/strong> \u65b9\u6cd5\u7528\u4e8e\u6bd4\u8f83\u5bf9\u8c61\u7684\u5927\u5c0f\uff0c\u4f46\u662f\u8fd9\u4e2a\u65b9\u6cd5\u7684\u4f7f\u7528\u4f1a\u5bfc\u81f4\u4ee3\u7801\u53d8\u5f97\u590d\u6742\u548c\u5bb9\u6613\u51fa\u9519\u3002\u4e3a\u4e86\u7edf\u4e00\u6bd4\u8f83\u5bf9\u8c61\u5927\u5c0f\u7684\u65b9\u5f0f\uff0cPython3\u5f15\u5165\u4e86 <strong>lt<\/strong>\u3001<strong>gt<\/strong>\u3001<strong>le<\/strong>\u3001<strong>ge<\/strong> \u8fd9\u51e0\u4e2a\u65b9\u6cd5\uff0c\u5206\u522b\u8868\u793a\u5c0f\u4e8e\u3001\u5927\u4e8e\u3001\u5c0f\u4e8e\u7b49\u4e8e\u3001\u5927\u4e8e\u7b49\u4e8e\u7684\u6bd4\u8f83\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u4e3a\u4ec0\u4e48\u6539\u7528 <strong>lt<\/strong>\u3001<strong>gt<\/strong> \u7b49\u65b9\u6cd5\u66ff\u4ee3 <strong>cmp<\/strong>?<\/strong><br \/>\n\u4f7f\u7528 <strong>lt<\/strong>\u3001<strong>gt<\/strong> \u7b49\u65b9\u6cd5\u66ff\u4ee3 <strong>cmp<\/strong> \u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u6613\u8bfb\u548c\u6613\u4e8e\u7406\u89e3\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u8fd0\u7b97\u7b26\uff08\u5982 &lt;\u3001&gt;\u3001&lt;=\u3001&gt;=\uff09\u8fdb\u884c\u6bd4\u8f83\uff0c\u800c\u65e0\u9700\u518d\u81ea\u5df1\u5b9a\u4e49 <strong>cmp<\/strong> \u65b9\u6cd5\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u4ee3\u7801\u5c31\u66f4\u52a0\u76f4\u89c2\uff0c\u964d\u4f4e\u4e86\u51fa\u9519\u7684\u6982\u7387\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd8\u6709\u5176\u4ed6\u65b9\u5f0f\u53ef\u4ee5\u8fdb\u884c\u5bf9\u8c61\u6bd4\u8f83\u5417?<\/strong><br \/>\n\u9664\u4e86\u4f7f\u7528 <strong>lt<\/strong>\u3001<strong>gt<\/strong>\u3001<strong>le<\/strong>\u3001<strong>ge<\/strong> \u7b49\u65b9\u6cd5\u8fdb\u884c\u5bf9\u8c61\u6bd4\u8f83\u5916\uff0cPython3\u8fd8\u63d0\u4f9b\u4e86\u53e6\u5916\u4e00\u79cd\u65b9\u5f0f\uff1a\u4f7f\u7528 functools \u6a21\u5757\u4e2d\u7684 total_ordering \u88c5\u9970\u5668\u3002\u4f7f\u7528\u8fd9\u4e2a\u88c5\u9970\u5668\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5b9a\u4e49 <strong>lt<\/strong> \u65b9\u6cd5\uff0c\u5c31\u53ef\u4ee5\u81ea\u52a8\u751f\u6210\u5176\u4ed6\u6bd4\u8f83\u65b9\u6cd5\u3002\u8fd9\u6837\u53ef\u4ee5\u51cf\u5c11\u4ee3\u7801\u7684\u91cd\u590d\u52b3\u52a8\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u603b\u7ed3\uff1a<br \/>\nPython3\u53d6\u6d88\u4e86 <strong>cmp<\/strong> \u65b9\u6cd5\u4e3b\u8981\u662f\u4e3a\u4e86\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u4e00\u81f4\u6027\uff0c\u91c7\u7528\u4e86 <strong>lt<\/strong>\u3001<strong>gt<\/strong>\u3001<strong>le<\/strong>\u3001<strong>ge<\/strong> \u7b49\u65b9\u6cd5\u66ff\u4ee3 <strong>cmp<\/strong>\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528 functools \u6a21\u5757\u4e2d\u7684 total_ordering \u88c5\u9970\u5668\u6765\u7b80\u5316\u5bf9\u8c61\u6bd4\u8f83\u7684\u4ee3\u7801\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python 3\u4e2d\uff0c\u53d6\u6d88\u4e86__cmp__\u65b9\u6cd5\uff0c\u4e3b\u8981\u662f\u4e3a\u4e86\u7b80\u5316\u5bf9\u8c61\u95f4\u6bd4\u8f83\u7684\u5b9e\u73b0\u5e76\u63d0\u9ad8\u5176\u7075\u6d3b\u6027\u548c\u6548\u7387\u3002\u5177\u4f53\u6765\u8bf4\uff0c [&hellip;]","protected":false},"author":3,"featured_media":178604,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/178589"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=178589"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/178589\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/178604"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=178589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=178589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=178589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}