{"id":2930,"date":"2025-06-03T16:28:42","date_gmt":"2025-06-04T09:22:41","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2930"},"modified":"2025-06-04T16:28:42","modified_gmt":"2025-06-04T09:22:41","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84eq%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%84eq%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 `__eq__` \u51fd\u6578\uff1a\u91cd\u8f09\u7b49\u65bc\u904b\u7b97\u7b26\u7684\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, __eq__(), \u91cd\u8f09\u985e, \u7b49\u65bc\u904b\u7b97\u7b26, Python \u6559\u5b78, Python \u51fd\u6578\"><\/p>\n<p>Python \u662f\u4e00\u7a2e\u975e\u5e38\u6d41\u884c\u4e14\u529f\u80fd\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\u3002\u8fd1\u5e74\u4f86\uff0c\u96a8\u8457 Python \u7684\u666e\u53ca\uff0c\u8a31\u591a\u958b\u767c\u8005\u958b\u59cb\u4f7f\u7528\u6b64\u8a9e\u8a00\u4f86\u89e3\u6c7a\u5404\u7a2e\u554f\u984c\u3002\u5728 Python \u4e2d\uff0c`__eq__()` \u51fd\u6578\u662f\u7528\u65bc\u91cd\u8f09\u985e\u7684\u7b49\u65bc\u904b\u7b97\u7b26\u7684\u91cd\u8981\u5de5\u5177\u3002\u900f\u904e\u9019\u500b\u51fd\u6578\uff0c\u6211\u5011\u53ef\u4ee5\u81ea\u5b9a\u7fa9\u5c0d\u8c61\u7684\u76f8\u7b49\u6027\u6bd4\u8f03\uff0c\u4e26\u8fd4\u56de\u5e03\u723e\u503c\u4ee5\u6307\u793a\u5c0d\u8c61\u662f\u5426\u76f8\u7b49\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f `__eq__()` \u51fd\u6578\uff1f<\/h2>\n<p>`__eq__()` \u51fd\u6578\u662f Python \u985e\u4e2d\u7684\u4e00\u500b\u7279\u6b8a\u65b9\u6cd5\uff0c\u5b83\u5728\u4f7f\u7528 `==` \u904b\u7b97\u7b26\u9032\u884c\u6bd4\u8f03\u6642\u88ab\u81ea\u52d5\u8abf\u7528\u3002\u7576\u5169\u500b\u5c0d\u8c61\u9032\u884c\u6bd4\u8f03\u6642\uff0cPython \u6703\u6aa2\u67e5\u9019\u4e9b\u5c0d\u8c61\u662f\u5426\u5be6\u73fe\u4e86 `__eq__()` \u65b9\u6cd5\uff0c\u4ee5\u78ba\u5b9a\u5b83\u5011\u662f\u5426\u76f8\u7b49\u3002<\/p>\n<h2> `__eq__()` \u51fd\u6578\u7684\u8a9e\u6cd5<\/h2>\n<p>`__eq__()` \u51fd\u6578\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\ndef __eq__(self, other):<br \/>\n    &#8230;<br \/>\n&#8220;`<\/p>\n<p>\u5176\u4e2d\uff0c`self` \u4ee3\u8868\u7576\u524d\u5c0d\u8c61\uff0c`other` \u4ee3\u8868\u8981\u6bd4\u8f03\u7684\u53e6\u4e00\u500b\u5c0d\u8c61\u3002<\/p>\n<h2> `__eq__()` \u51fd\u6578\u7684\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5728\u81ea\u5b9a\u7fa9\u985e\u4e2d\u4f7f\u7528 `__eq__()` \u51fd\u6578\u4f86\u6bd4\u8f03\u5c0d\u8c61\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass Person:<br \/>\n    def __init__(self, name, age):<br \/>\n        self.name = name<br \/>\n        self.age = age<\/p>\n<p>    def __eq__(self, other):<br \/>\n        if isinstance(other, Person):<br \/>\n            return self.name == other.name and self.age == other.age<br \/>\n        return False<\/p>\n<p># \u5b9a\u7fa9\u5169\u500b\u5c0d\u8c61<br \/>\nperson1 = Person(&#8220;Alice&#8221;, 30)<br \/>\nperson2 = Person(&#8220;Alice&#8221;, 30)<br \/>\nperson3 = Person(&#8220;Bob&#8221;, 25)<\/p>\n<p># \u6bd4\u8f03\u5169\u500b\u5c0d\u8c61<br \/>\nprint(person1 == person2)  # \u8f38\u51fa: True<br \/>\nprint(person1 == person3)  # \u8f38\u51fa: False<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b `Person` \u985e\uff0c\u4e26\u91cd\u8f09\u4e86 `__eq__()` \u65b9\u6cd5\u4f86\u6bd4\u8f03\u5169\u500b `Person` \u5c0d\u8c61\u7684\u540d\u7a31\u548c\u5e74\u9f61\u3002\u9019\u6a23\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 `==` \u904b\u7b97\u7b26\u4f86\u6bd4\u8f03\u9019\u4e9b\u5c0d\u8c61\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `__eq__()` \u51fd\u6578\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>1. **\u985e\u578b\u932f\u8aa4**\uff1a\u78ba\u4fdd\u5728\u6bd4\u8f03\u6642\u5c0d\u8c61\u7684\u985e\u578b\u6b63\u78ba\uff0c\u5426\u5247\u6703\u5c0e\u81f4\u6bd4\u8f03\u4e0d\u6b63\u78ba\u3002<br \/>\n2. **\u7121\u6cd5\u6bd4\u8f03\u7684\u5c0d\u8c61**\uff1a\u5982\u679c\u5c0d\u8c61\u4e0d\u5177\u5099\u53ef\u6bd4\u8f03\u7684\u5c6c\u6027\uff08\u5982\u672a\u5b9a\u7fa9\u7684\u5c6c\u6027\uff09\uff0c\u5247\u61c9\u8a72\u6dfb\u52a0\u76f8\u61c9\u7684\u932f\u8aa4\u8655\u7406\u908f\u8f2f\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86 `__eq__()`\uff0cPython \u9084\u63d0\u4f9b\u4e86\u5176\u4ed6\u7279\u6b8a\u65b9\u6cd5\u4f86\u91cd\u8f09\u904b\u7b97\u7b26\uff0c\u4f8b\u5982 `__lt__()`\uff08\u5c0f\u65bc\u904b\u7b97\u7b26\uff09\u3001`__gt__()`\uff08\u5927\u65bc\u904b\u7b97\u7b26\uff09\u7b49\u3002\u9019\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u8207 `__eq__()` \u4e00\u8d77\u4f7f\u7528\uff0c\u4ee5\u5be6\u73fe\u66f4\u5168\u9762\u7684\u5c0d\u8c61\u6bd4\u8f03\u529f\u80fd\u3002<\/p>\n<p>\u5982\u679c\u4f60\u60f3\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u904b\u7b97\u7b26\u91cd\u8f09\uff0c\u8acb\u53c3\u8003\u9019\u7bc7 [Python \u904b\u7b97\u7b26\u91cd\u8f09\u6559\u5b78](https:\/\/vocus.cc\/article\/63e1e8c9fd89780001b5898e)\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>`__eq__()` \u51fd\u6578\u662f Python \u4e2d\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u529f\u80fd\uff0c\u5b83\u4f7f\u6211\u5011\u80fd\u5920\u81ea\u5b9a\u7fa9\u5c0d\u8c61\u7684\u76f8\u7b49\u6027\u908f\u8f2f\u3002\u901a\u904e\u91cd\u8f09\u9019\u500b\u904b\u7b97\u7b26\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u6211\u5011\u7684\u985e\u66f4\u5177\u9748\u6d3b\u6027\u548c\u53ef\u7528\u6027\u3002\u5b78\u7fd2\u5982\u4f55\u6b63\u78ba\u5be6\u73fe `__eq__()` \u4e0d\u50c5\u80fd\u63d0\u9ad8\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\uff0c\u9084\u80fd\u5e6b\u52a9\u6211\u5011\u66f4\u597d\u5730\u7ba1\u7406\u8907\u96dc\u5c0d\u8c61\u7684\u6bd4\u8f03\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: \u70ba\u4ec0\u9ebc\u9700\u8981\u91cd\u8f09 `__eq__()` \u51fd\u6578\uff1f**<br \/>\nA1: \u91cd\u8f09 `__eq__()` \u51fd\u6578\u5141\u8a31\u6211\u5011\u81ea\u5b9a\u7fa9\u5c0d\u8c61\u7684\u76f8\u7b49\u6027\u908f\u8f2f\uff0c\u4f7f\u5f97 `==` \u904b\u7b97\u7b26\u80fd\u5920\u6839\u64da\u8a72\u908f\u8f2f\u6b63\u78ba\u6bd4\u8f03\u5c0d\u8c61\u3002<\/p>\n<p>**Q2: \u5982\u679c\u4e0d\u91cd\u8f09 `__eq__()`\uff0c\u6703\u767c\u751f\u4ec0\u9ebc\uff1f**<br \/>\nA2: \u5982\u679c\u4e0d\u91cd\u8f09 `__eq__()`\uff0cPython \u5c07\u4f7f\u7528\u9ed8\u8a8d\u7684\u5c0d\u8c61\u6bd4\u8f03\u65b9\u6cd5\uff0c\u9019\u901a\u5e38\u662f\u901a\u904e\u5c0d\u8c61\u7684\u5167\u5b58\u5730\u5740\u4f86\u5224\u65b7\u76f8\u7b49\u6027\uff0c\u9019\u53ef\u80fd\u4e0d\u7b26\u5408\u6211\u5011\u7684\u9700\u6c42\u3002<\/p>\n<p>**Q3: \u5982\u4f55\u8655\u7406 `None` \u6216\u5176\u4ed6\u985e\u578b\u7684\u6bd4\u8f03\uff1f**<br \/>\nA3: \u5728 `__eq__()` \u65b9\u6cd5\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 `isinstance()` \u4f86\u6aa2\u67e5 `other` \u662f\u5426\u70ba\u9810\u671f\u7684\u985e\u578b\uff0c\u4e26\u8fd4\u56de\u9069\u7576\u7684\u5e03\u723e\u503c\uff0c\u907f\u514d\u985e\u578b\u932f\u8aa4\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u7684eq()\u51fd\u6578\u53ef\u4ee5\u91cd\u8f09\u985e\u7684\u7b49\u65bc\u904b\u7b97\u7b26\uff0c\u53ef\u4ee5\u7528\u65bc\u6bd4\u8f03\u5169\u500b\u5c0d\u8c61\u662f\u5426\u76f8\u7b49\uff0c\u4e26\u63d0\u4f9b\u66f4\u591a\u7684\u6bd4\u8f03\u529f\u80fd\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39Python\u4e2d\u7684eq()\u51fd\u6578\uff0c\u4ee5\u53ca\u5b83\u7684\u4f7f\u7528\u65b9\u6cd5\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-2930","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-Lg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2930","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=2930"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2930\/revisions"}],"predecessor-version":[{"id":2931,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2930\/revisions\/2931"}],"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=2930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}