{"id":2928,"date":"2025-05-31T17:17:17","date_gmt":"2025-06-04T09:23:16","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2928"},"modified":"2025-06-04T17:17:17","modified_gmt":"2025-06-04T09:23:16","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84le%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84le%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u77ad\u89e3 Python \u4e2d\u7684 `__le__()` \u51fd\u6578\uff1a\u91cd\u8f09\u5c0f\u65bc\u7b49\u65bc\u904b\u7b97\u7b26\u7684\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, __le__(), \u91cd\u8f09\u985e, \u5c0f\u65bc\u7b49\u65bc\u904b\u7b97\u7b26\"><\/p>\n<h1>\u6df1\u5165\u77ad\u89e3 Python \u4e2d\u7684 `__le__()` \u51fd\u6578\uff1a\u91cd\u8f09\u5c0f\u65bc\u7b49\u65bc\u904b\u7b97\u7b26\u7684\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`__le__()` \u51fd\u6578\u662f\u7528\u65bc\u91cd\u8f09\u985e\u7684\u5c0f\u65bc\u7b49\u65bc\u904b\u7b97\u7b26\uff08`<=`\uff09\u7684\u7279\u6b8a\u65b9\u6cd5\u3002\u9019\u4f7f\u5f97\u6211\u5011\u53ef\u4ee5\u5b9a\u7fa9\u81ea\u5b9a\u7fa9\u5c0d\u8c61\u7684\u6bd4\u8f03\u884c\u70ba\u3002\u4ee5\u4e0b\u662f `__le__()` \u7684\u57fa\u672c\u8a9e\u6cd5\uff1a\n\n```python\nclass MyClass:\n    def __le__(self, other):\n        # \u5be6\u4f5c\u6bd4\u8f03\u908f\u8f2f\n```\n\n### \u57fa\u672c\u7528\u6cd5\n\n`__le__()` \u65b9\u6cd5\u63a5\u53d7\u5169\u500b\u5c0d\u8c61\u9032\u884c\u6bd4\u8f03\uff0c\u82e5\u7b2c\u4e00\u4e2a\u5bf9\u8c61\u5c0f\u65bc\u6216\u7b49\u65bc\u7b2c\u4e8c\u4e2a\u5bf9\u8c61\uff0c\u5247\u8fd4\u56de `True`\uff0c\u5426\u5247\u8fd4\u56de `False`\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a\n\n#### \u6578\u5b57\u6bd4\u8f03\n\n```python\nclass Number:\n    def __init__(self, value):\n        self.value = value\n    \n    def __le__(self, other):\n        return self.value <= other.value\n\nx = Number(5)\ny = Number(10)\n\nif x <= y:\n    print(\"x\u5c0f\u65bc\u7b49\u65bcy\")\nelse:\n    print(\"x\u5927\u65bcy\")\n```\n\n\u8f38\u51fa\u7d50\u679c\uff1a\n\n```\nx\u5c0f\u65bc\u7b49\u65bcy\n```\n\n#### \u5b57\u7b26\u4e32\u6bd4\u8f03\n\n```python\nclass MyString:\n    def __init__(self, value):\n        self.value = value\n    \n    def __le__(self, other):\n        return self.value <= other.value\n\nstr1 = MyString(\"Hello\")\nstr2 = MyString(\"World\")\n\nif str1 <= str2:\n    print(\"str1\u5c0f\u65bc\u7b49\u65bcstr2\")\nelse:\n    print(\"str1\u5927\u65bcstr2\")\n```\n\n\u8f38\u51fa\u7d50\u679c\uff1a\n\n```\nstr1\u5c0f\u65bc\u7b49\u65bcstr2\n```\n\n#### \u5217\u8868\u6bd4\u8f03\n\n```python\nclass MyList:\n    def __init__(self, items):\n        self.items = items\n    \n    def __le__(self, other):\n        return self.items <= other.items\n\nlist1 = MyList([1, 2, 3])\nlist2 = MyList([1, 2, 4])\n\nif list1 <= list2:\n    print(\"list1\u5c0f\u65bc\u7b49\u65bclist2\")\nelse:\n    print(\"list1\u5927\u65bclist2\")\n```\n\n\u8f38\u51fa\u7d50\u679c\uff1a\n\n```\nlist1\u5c0f\u65bc\u7b49\u65bclist2\n```\n\n### `__le__()` \u51fd\u6578\u7684\u512a\u9ede\n\n`__le__()` \u51fd\u6578\u7684\u6700\u5927\u512a\u9ede\u5728\u65bc\u5b83\u80fd\u5920\u901a\u904e\u81ea\u5b9a\u7fa9\u985e\u4f86\u6bd4\u8f03\u4e0d\u540c\u985e\u578b\u7684\u5c0d\u8c61\u3002\u9019\u63d0\u4f9b\u4e86\u9748\u6d3b\u6027\uff0c\u8b93\u958b\u767c\u8005\u80fd\u5920\u5728\u66f4\u8907\u96dc\u7684\u61c9\u7528\u4e2d\u5be6\u73fe\u81ea\u5b9a\u7fa9\u908f\u8f2f\u3002\n\n\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u64f4\u5c55\u6bd4\u8f03\u908f\u8f2f\u4f86\u652f\u6301\u591a\u500b\u5c0d\u8c61\u7684\u6bd4\u8f03\uff1a\n\n```python\nclass MultiCompare:\n    def __init__(self, values):\n        self.values = values\n    \n    def __le__(self, other):\n        return all(v <= o for v, o in zip(self.values, other.values))\n\nx = MultiCompare([5, 10])\ny = MultiCompare([10, 15])\n\nif x <= y:\n    print(\"x\u5c0f\u65bc\u7b49\u65bcy\")\nelse:\n    print(\"x\u5927\u65bcy\")\n```\n\n\u8f38\u51fa\u7d50\u679c\uff1a\n\n```\nx\u5c0f\u65bc\u7b49\u65bcy\n```\n\n### \u7e3d\u7d50\n\n`__le__()` \u51fd\u6578\u662f\u4e00\u500b\u529f\u80fd\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u65bc\u6bd4\u8f03\u5404\u7a2e\u5c0d\u8c61\u3002\u900f\u904e\u91cd\u8f09\u6b64\u904b\u7b97\u7b26\uff0c\u60a8\u80fd\u5920\u5b9a\u7fa9\u81ea\u5b9a\u7fa9\u5c0d\u8c61\u7684\u6bd4\u8f03\u908f\u8f2f\uff0c\u5f9e\u800c\u4f7f\u4ee3\u78bc\u66f4\u52a0\u9748\u6d3b\u4e14\u6613\u65bc\u7dad\u8b77\u3002\u8981\u6df1\u5165\u4e86\u89e3 Python \u7684\u5176\u4ed6\u7279\u6b8a\u65b9\u6cd5\uff0c\u8acb\u53c3\u8003 [Python \u5b98\u65b9\u6587\u6a94](https:\/\/docs.python.org\/3\/reference\/datamodel.html#special-method-names)\u3002\n\n### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09\n\n**Q1: `__le__()` \u548c `__lt__()` \u6709\u4ec0\u9ebc\u5340\u5225\uff1f**  \nA1: `__le__()` \u7528\u65bc\u6bd4\u8f03\u5c0f\u65bc\u7b49\u65bc\uff0c\u800c `__lt__()` \u7528\u65bc\u6bd4\u8f03\u5c0f\u65bc\u3002\u9019\u5169\u8005\u53ef\u4ee5\u4e00\u8d77\u4f7f\u7528\u4ee5\u5b9a\u7fa9\u5b8c\u6574\u7684\u6bd4\u8f03\u884c\u70ba\u3002\n\n**Q2: \u53ef\u4ee5\u5728 `__le__()` \u4e2d\u9032\u884c\u66f4\u8907\u96dc\u7684\u6bd4\u8f03\u908f\u8f2f\u55ce\uff1f**  \nA2: \u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u5be6\u73fe\u8907\u96dc\u7684\u908f\u8f2f\uff0c\u5305\u62ec\u6bd4\u8f03\u591a\u500b\u5c6c\u6027\u6216\u4e0d\u540c\u985e\u578b\u7684\u5c0d\u8c61\u3002\n\n**Q3: \u5982\u4f55\u8655\u7406\u985e\u578b\u4e0d\u5339\u914d\u7684\u60c5\u6cc1\uff1f**  \nA3: \u5728 `__le__()` \u65b9\u6cd5\u4e2d\uff0c\u60a8\u53ef\u4ee5\u6dfb\u52a0\u985e\u578b\u6aa2\u67e5\u4ee5\u78ba\u4fdd\u6b63\u78ba\u6bd4\u8f03\uff0c\u4f8b\u5982\u4f7f\u7528 `isinstance()` \u51fd\u6578\u3002\n\n---\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u7684le()\u51fd\u6578\u662f\u7528\u65bc\u91cd\u8f09\u985e\u7684\u5c0f\u65bc\u7b49\u65bc\u904b\u7b97\u7b26\uff0c\u5b83\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u6bd4\u8f03\u5169\u500b\u5c0d\u8c61\u7684\u5927\u5c0f\uff0c\u4e26\u63d0\u4f9b\u66f4\u591a\u7684\u63a7\u5236\u529f\u80fd\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39le()\u51fd\u6578\u7684\u529f\u80fd\uff0c\u4ee5\u53ca\u5982\u4f55\u5728Python\u4e2d\u4f7f\u7528\u5b83\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-2928","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-Le","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=2928"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2928\/revisions"}],"predecessor-version":[{"id":13011,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2928\/revisions\/13011"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=2928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}