{"id":2926,"date":"2025-05-29T16:34:15","date_gmt":"2025-06-04T09:24:15","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2926"},"modified":"2025-06-04T16:34:15","modified_gmt":"2025-06-04T09:24:15","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84lt%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%84lt%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3Python\u4e2d\u7684 `__lt__()` \u51fd\u6578\uff1a\u91cd\u8f09\u5c0f\u65bc\u904b\u7b97\u7b26\u7684\u6700\u65b0\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, __lt__(), \u91cd\u8f09\u985e, \u5c0f\u65bc\u904b\u7b97\u7b26, Python \u6559\u5b78, Python \u7a0b\u5f0f\u8a2d\u8a08\"><\/p>\n<p>Python\u662f\u4e00\u7a2e\u975e\u5e38\u6d41\u884c\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u5b83\u63d0\u4f9b\u4e86\u8a31\u591a\u5f37\u5927\u7684\u529f\u80fd\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f `__lt__()` \u51fd\u6578\uff0c\u5b83\u7528\u65bc\u91cd\u8f09\u985e\u7684\u5c0f\u65bc\u904b\u7b97\u7b26 (`<`)\u3002\u9019\u500b\u7279\u6b8a\u65b9\u6cd5\u4f7f\u5f97\u6211\u5011\u80fd\u5920\u81ea\u5b9a\u7fa9\u5c0d\u8c61\u7684\u6bd4\u8f03\u884c\u70ba\uff0c\u4e26\u5728\u5c0d\u8c61\u4e4b\u9593\u9032\u884c\u6bd4\u8f03\u6642\u8fd4\u56de\u5e03\u723e\u503c\uff0c\u8868\u793a\u4e00\u500b\u5c0d\u8c61\u662f\u5426\u5c0f\u65bc\u53e6\u4e00\u500b\u5c0d\u8c61\u3002\n\n### `__lt__()` \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5\n\n`__lt__()` \u51fd\u6578\u7684\u4f7f\u7528\u975e\u5e38\u7c21\u55ae\uff0c\u7576\u6211\u5011\u5728\u81ea\u5b9a\u7fa9\u985e\u4e2d\u91cd\u8f09\u5c0f\u65bc\u904b\u7b97\u7b26\u6642\uff0c\u53ea\u9700\u8981\u5728\u985e\u4e2d\u5be6\u73fe\u6b64\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 `__lt__()` \u51fd\u6578\u4f86\u6bd4\u8f03\u5169\u500b\u81ea\u5b9a\u7fa9\u5c0d\u8c61\uff1a\n\n```python\nclass Person:\n    def __init__(self, name, age):\n        self.name = name\n        self.age = age\n\n    def __lt__(self, other):\n        return self.age < other.age\n\n# \u4f7f\u7528\u7bc4\u4f8b\nalice = Person(\"Alice\", 30)\nbob = Person(\"Bob\", 25)\n\nif alice < bob:\n    print(f\"{alice.name} is younger than {bob.name}\")\nelse:\n    print(f\"{alice.name} is not younger than {bob.name}\")\n```\n\n\u5728\u4e0a\u8ff0\u4ee3\u78bc\u4e2d\uff0c`Person` \u985e\u91cd\u8f09\u4e86 `__lt__()` \u51fd\u6578\uff0c\u9019\u6a23\u5c31\u53ef\u4ee5\u4f7f\u7528 `<` \u904b\u7b97\u7b26\u4f86\u6bd4\u8f03\u5169\u500b `Person` \u5c0d\u8c61\u7684\u5e74\u9f61\u3002\n\n### \u6bd4\u8f03\u57fa\u672c\u6578\u64da\u985e\u578b\n\n\u9664\u4e86\u81ea\u5b9a\u7fa9\u985e\uff0c`__lt__()` \u51fd\u6578\u4e5f\u53ef\u4ee5\u7528\u65bc\u6bd4\u8f03\u57fa\u672c\u6578\u64da\u985e\u578b\uff0c\u5982\u6574\u6578\u3001\u5b57\u7b26\u4e32\u548c\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff1a\n\n#### \u6bd4\u8f03\u6574\u6578\n\n```python\nx = 5\ny = 10\n\nif x < y:\n    print(\"x is less than y\")\nelse:\n    print(\"x is not less than y\")\n```\n\n#### \u6bd4\u8f03\u5b57\u7b26\u4e32\n\n```python\nstr1 = \"Apple\"\nstr2 = \"Banana\"\n\nif str1 < str2:\n    print(f\"{str1} is less than {str2}\")\nelse:\n    print(f\"{str1} is not less than {str2}\")\n```\n\n#### \u6bd4\u8f03\u5217\u8868\n\n```python\nlist1 = [1, 2, 3]\nlist2 = [4, 5, 6]\n\nif list1 < list2:\n    print(\"list1 is less than list2\")\nelse:\n    print(\"list1 is not less than list2\")\n```\n\n### \u932f\u8aa4\u6392\u9664\n\n\u5728\u4f7f\u7528 `__lt__()` \u51fd\u6578\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u985e\u578b\u932f\u8aa4\uff0c\u7279\u5225\u662f\u7576\u5169\u500b\u5c0d\u8c61\u662f\u4e0d\u540c\u985e\u578b\u6642\u3002\u70ba\u4e86\u907f\u514d\u9019\u4e9b\u932f\u8aa4\uff0c\u53ef\u4ee5\u5728 `__lt__()` \u65b9\u6cd5\u4e2d\u6dfb\u52a0\u985e\u578b\u6aa2\u67e5\uff1a\n\n```python\ndef __lt__(self, other):\n    if not isinstance(other, Person):\n        return NotImplemented\n    return self.age < other.age\n```\n\n### \u5ef6\u4f38\u61c9\u7528\n\n`__lt__()` \u51fd\u6578\u4e0d\u50c5\u9650\u65bc\u7c21\u55ae\u7684\u6bd4\u8f03\uff0c\u9084\u53ef\u4ee5\u5728\u6392\u5e8f\u6642\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 `sorted()` \u51fd\u6578\u6642\uff0c\u6703\u81ea\u52d5\u8abf\u7528 `__lt__()` \u65b9\u6cd5\uff1a\n\n```python\npeople = [alice, bob]\nsorted_people = sorted(people)\nfor person in sorted_people:\n    print(person.name)\n```\n\n\u9019\u5c07\u6839\u64da\u5e74\u9f61\u5c0d `people` \u5217\u8868\u9032\u884c\u6392\u5e8f\u3002\n\n\u7e3d\u7d50\u4f86\u8aaa\uff0c`__lt__()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u5b83\u5141\u8a31\u6211\u5011\u81ea\u5b9a\u7fa9\u5c0d\u8c61\u7684\u6bd4\u8f03\u884c\u70ba\u3002\u7121\u8ad6\u662f\u6bd4\u8f03\u81ea\u5b9a\u7fa9\u985e\u9084\u662f\u57fa\u672c\u6578\u64da\u985e\u578b\uff0c`__lt__()` \u90fd\u80fd\u63d0\u4f9b\u7c21\u6f54\u660e\u4e86\u7684\u89e3\u6c7a\u65b9\u6848\u3002\n\n\u82e5\u60f3\u6df1\u5165\u4e86\u89e3 Python \u7684\u66f4\u591a\u5167\u5bb9\uff0c\u60a8\u53ef\u4ee5\u53c3\u8003\u9019\u7bc7\u6587\u7ae0\uff1a[Python \u7a0b\u5f0f\u8a2d\u8a08\u57fa\u790e](https:\/\/vocus.cc\/article\/60a2b3d9fd89780001f4d3f1)\u3002\n\n### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09\n\n**Q1: `__lt__()` \u548c\u5176\u4ed6\u6bd4\u8f03\u904b\u7b97\u7b26\u6709\u4f55\u4e0d\u540c\uff1f**  \nA1: `__lt__()` \u5c08\u9580\u7528\u65bc\u91cd\u8f09\u5c0f\u65bc\u904b\u7b97\u7b26 (`<`)\uff0c\u800c\u5176\u4ed6\u6bd4\u8f03\u904b\u7b97\u7b26\u5982 `__gt__()`\uff08\u5927\u65bc\uff09\u3001`__eq__()`\uff08\u7b49\u65bc\uff09\u7b49\u9700\u8981\u5206\u5225\u5be6\u73fe\u3002\n\n**Q2: \u6211\u53ef\u4ee5\u5728\u540c\u4e00\u500b\u985e\u4e2d\u540c\u6642\u91cd\u8f09\u591a\u500b\u6bd4\u8f03\u904b\u7b97\u7b26\u55ce\uff1f**  \nA2: \u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u5728\u985e\u4e2d\u540c\u6642\u5be6\u73fe `__lt__()`\u3001`__gt__()`\u3001`__eq__()` \u7b49\u591a\u500b\u65b9\u6cd5\uff0c\u4ee5\u652f\u6301\u5404\u7a2e\u6bd4\u8f03\u64cd\u4f5c\u3002\n\n**Q3: \u662f\u5426\u6240\u6709\u985e\u578b\u90fd\u53ef\u4ee5\u4f7f\u7528 `__lt__()` \u9032\u884c\u6bd4\u8f03\uff1f**  \nA3: \u4e0d\u662f\uff0c\u53ea\u6709\u7576\u60a8\u5728\u985e\u4e2d\u5be6\u73fe\u4e86 `__lt__()` \u65b9\u6cd5\u6642\uff0c\u624d\u80fd\u4f7f\u7528 `<` \u9032\u884c\u6bd4\u8f03\u3002\u82e5\u6bd4\u8f03\u4e0d\u540c\u985e\u578b\u7684\u5c0d\u8c61\uff0c\u61c9\u8a72\u9032\u884c\u985e\u578b\u6aa2\u67e5\u4ee5\u907f\u514d\u932f\u8aa4\u3002\n---\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>lt()\u51fd\u6578\u662fPython\u4e2d\u7528\u65bc\u91cd\u8f09\u985e\u7684\u5c0f\u65bc\u904b\u7b97\u7b26\uff0c\u5b83\u53ef\u4ee5\u6bd4\u8f03\u5169\u500b\u5c0d\u8c61\u7684\u5927\u5c0f\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u5e03\u723e\u503c\uff0c\u4ee5\u6307\u793a\u5169\u500b\u5c0d\u8c61\u662f\u5426\u76f8\u7b49\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39lt()\u51fd\u6578\u7684\u7528\u6cd5\uff0c\u4ee5\u53ca\u5b83\u5728Python\u4e2d\u7684\u61c9\u7528\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-2926","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-Lc","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2926","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=2926"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2926\/revisions"}],"predecessor-version":[{"id":2927,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2926\/revisions\/2927"}],"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=2926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}