{"id":2900,"date":"2025-05-30T17:11:57","date_gmt":"2025-06-04T09:27:56","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2900"},"modified":"2025-06-04T17:11:57","modified_gmt":"2025-06-04T09:27:56","slug":"python%e4%b8%ad%e7%9a%84%e5%8a%a0%e6%b3%95%e9%81%8b%e7%ae%97%e7%ac%a6add%e7%9a%84%e9%87%8d%e8%bc%89","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python%e4%b8%ad%e7%9a%84%e5%8a%a0%e6%b3%95%e9%81%8b%e7%ae%97%e7%ac%a6add%e7%9a%84%e9%87%8d%e8%bc%89\/","title":{"rendered":"Python \u4e2d\u7684\u52a0\u6cd5\u904b\u7b97\u7b26\u91cd\u8f09\uff1a\u4f7f\u7528 `__add__()` \u65b9\u6cd5\u7684\u8a73\u7d30\u6559\u5b78"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, __add__(), \u91cd\u8f09\u985e, \u52a0\u6cd5\u904b\u7b97\u7b26\"><\/p>\n<p>Python \u662f\u4e00\u7a2e\u975e\u5e38\u6d41\u884c\u4e14\u529f\u80fd\u5f37\u5927\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u65b9\u6cd5\u4f86\u91cd\u8f09\u904b\u7b97\u7b26\uff0c\u4f7f\u5f97\u958b\u767c\u8005\u53ef\u4ee5\u5b9a\u7fa9\u81ea\u8a02\u7684\u884c\u70ba\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u4f7f\u7528 `__add__()` \u65b9\u6cd5\u4f86\u91cd\u8f09\u52a0\u6cd5\u904b\u7b97\u7b26\uff0c\u4e26\u63d0\u4f9b\u5be6\u4f5c\u7bc4\u4f8b\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<h2>\u91cd\u8f09\u52a0\u6cd5\u904b\u7b97\u7b26\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u5728 Python \u4e2d\uff0c\u91cd\u8f09\u904b\u7b97\u7b26\u662f\u6307\u5b9a\u7fa9\u7269\u4ef6\u5728\u7279\u5b9a\u904b\u7b97\u7b26\uff08\u5982 +\u3001-\u3001* \u7b49\uff09\u4e0b\u7684\u884c\u70ba\u3002\u5c0d\u65bc\u52a0\u6cd5\u904b\u7b97\u7b26\uff0c\u6211\u5011\u901a\u5e38\u4f7f\u7528 `__add__()` \u65b9\u6cd5\u4f86\u5be6\u73fe\u3002\u9019\u500b\u65b9\u6cd5\u5141\u8a31\u6211\u5011\u5b9a\u7fa9\u7576\u5169\u500b\u7269\u4ef6\u76f8\u52a0\u6642\u767c\u751f\u4ec0\u9ebc\u4e8b\u60c5\u3002<\/p>\n<h2>\u4f7f\u7528 `__add__()` \u65b9\u6cd5\u7684\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u4f8b\u5b50\uff0c\u5c55\u793a\u5982\u4f55\u91cd\u8f09\u52a0\u6cd5\u904b\u7b97\u7b26\u4ee5\u5408\u4f75\u5169\u500b\u81ea\u8a02\u7684\u985e\u5225\u7269\u4ef6\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyObject:<br \/>\n    def __init__(self, value):<br \/>\n        self.value = value<\/p>\n<p>    def __add__(self, other):<br \/>\n        if isinstance(other, MyObject):<br \/>\n            return MyObject(self.value + other.value)<br \/>\n        return NotImplemented<\/p>\n<p># \u4f7f\u7528\u7bc4\u4f8b<br \/>\nobj1 = MyObject(10)<br \/>\nobj2 = MyObject(20)<br \/>\nobj3 = obj1 + obj2  # \u9019\u88e1\u6703\u547c\u53eb obj1.__add__(obj2)<\/p>\n<p>print(obj3.value)  # \u8f38\u51fa\uff1a30<br \/>\n&#8220;`<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b\u540d\u70ba `MyObject` \u7684\u985e\u5225\uff0c\u4e26\u5728\u5176\u4e2d\u91cd\u8f09\u4e86 `__add__()` \u65b9\u6cd5\u3002\u7576\u6211\u5011\u5c07\u5169\u500b `MyObject` \u7269\u4ef6\u76f8\u52a0\u6642\uff0c\u5c07\u8fd4\u56de\u4e00\u500b\u65b0\u7684 `MyObject` \u7269\u4ef6\uff0c\u4e26\u5c07\u5b83\u5011\u7684 `value` \u503c\u76f8\u52a0\u3002<\/p>\n<h2>\u8655\u7406\u932f\u8aa4\u8207\u4f8b\u5916<\/h2>\n<p>\u5728\u91cd\u8f09\u904b\u7b97\u7b26\u6642\uff0c\u8655\u7406\u932f\u8aa4\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u5728 `__add__()` \u65b9\u6cd5\u4e2d\uff0c\u6211\u5011\u4f7f\u7528 `isinstance()` \u6aa2\u67e5\u53e6\u4e00\u500b\u7269\u4ef6\u7684\u985e\u578b\uff0c\u4e26\u5728\u4e0d\u5339\u914d\u6642\u8fd4\u56de `NotImplemented`\u3002\u9019\u662f Python \u7684\u4e00\u500b\u6700\u4f73\u5be6\u8e10\uff0c\u5141\u8a31\u5176\u4ed6\u985e\u5225\u9032\u884c\u9069\u7576\u7684\u8655\u7406\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u6578\u503c\u76f8\u52a0\uff0c\u91cd\u8f09\u52a0\u6cd5\u904b\u7b97\u7b26\u9084\u53ef\u4ee5\u61c9\u7528\u65bc\u66f4\u8907\u96dc\u7684\u8cc7\u6599\u7d50\u69cb\uff0c\u4f8b\u5982\u5408\u4f75\u5b57\u5178\u6216\u5217\u8868\u3002\u4ee5\u4e0b\u662f\u5b57\u5178\u5408\u4f75\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyDict:<br \/>\n    def __init__(self, data):<br \/>\n        self.data = data<\/p>\n<p>    def __add__(self, other):<br \/>\n        if isinstance(other, MyDict):<br \/>\n            new_data = {**self.data, **other.data}  # \u5408\u4f75\u5b57\u5178<br \/>\n            return MyDict(new_data)<br \/>\n        return NotImplemented<\/p>\n<p># \u4f7f\u7528\u7bc4\u4f8b<br \/>\ndict1 = MyDict({&#8216;key1&#8217;: &#8216;value1&#8217;, &#8216;key2&#8217;: &#8216;value2&#8217;})<br \/>\ndict2 = MyDict({&#8216;key3&#8217;: &#8216;value3&#8217;, &#8216;key4&#8217;: &#8216;value4&#8217;})<br \/>\ndict3 = dict1 + dict2  # \u9019\u88e1\u6703\u547c\u53eb dict1.__add__(dict2)<\/p>\n<p>print(dict3.data)  # \u8f38\u51fa\uff1a{&#8216;key1&#8217;: &#8216;value1&#8217;, &#8216;key2&#8217;: &#8216;value2&#8217;, &#8216;key3&#8217;: &#8216;value3&#8217;, &#8216;key4&#8217;: &#8216;value4&#8217;}<br \/>\n&#8220;`<\/p>\n<p>\u9019\u500b\u7bc4\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528 `__add__()` \u65b9\u6cd5\u5408\u4f75\u5169\u500b\u81ea\u8a02\u5b57\u5178\u7269\u4ef6\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u91cd\u8f09\u52a0\u6cd5\u904b\u7b97\u7b26\u662f Python \u4e2d\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u529f\u80fd\uff0c\u80fd\u5920\u8b93\u958b\u767c\u8005\u5b9a\u7fa9\u7269\u4ef6\u7684\u52a0\u6cd5\u884c\u70ba\u3002\u900f\u904e `__add__()` \u65b9\u6cd5\uff0c\u6211\u5011\u53ef\u4ee5\u9748\u6d3b\u5730\u5be6\u73fe\u5404\u7a2e\u904b\u7b97\uff0c\u4e26\u7c21\u5316\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u5176\u4ed6\u904b\u7b97\u7b26\u91cd\u8f09\uff0c\u60a8\u53ef\u4ee5\u53c3\u8003\u6211\u5011\u7684 [Python \u904b\u7b97\u7b26\u91cd\u8f09\u6559\u5b78](https:\/\/vocus.cc\/article\/605a1f2f5f4c2f001c2b7e3d)\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: Python \u4e2d\u70ba\u4ec0\u9ebc\u8981\u91cd\u8f09\u904b\u7b97\u7b26\uff1f**<br \/>\nA1: \u91cd\u8f09\u904b\u7b97\u7b26\u53ef\u4ee5\u8b93\u81ea\u8a02\u7269\u4ef6\u4e4b\u9593\u7684\u64cd\u4f5c\u66f4\u52a0\u81ea\u7136\uff0c\u63d0\u5347\u7a0b\u5f0f\u7684\u53ef\u8b80\u6027\u548c\u6613\u7528\u6027\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u8655\u7406\u4e0d\u652f\u6301\u7684\u985e\u578b\uff1f**<br \/>\nA2: \u5728 `__add__()` \u65b9\u6cd5\u4e2d\u61c9\u4f7f\u7528 `isinstance()` \u4f86\u6aa2\u67e5\u50b3\u5165\u7269\u4ef6\u7684\u985e\u578b\uff0c\u4e26\u8fd4\u56de `NotImplemented` \u4ee5\u5141\u8a31\u5176\u4ed6\u985e\u5225\u9032\u884c\u9069\u7576\u8655\u7406\u3002<\/p>\n<p>**Q3: \u9664\u4e86\u52a0\u6cd5\uff0c\u9084\u53ef\u4ee5\u91cd\u8f09\u54ea\u4e9b\u904b\u7b97\u7b26\uff1f**<br \/>\nA3: Python \u652f\u6301\u591a\u7a2e\u904b\u7b97\u7b26\u7684\u91cd\u8f09\uff0c\u5305\u62ec\u6e1b\u6cd5\uff08`__sub__()`\uff09\u3001\u4e58\u6cd5\uff08`__mul__()`\uff09\u3001\u9664\u6cd5\uff08`__truediv__()`\uff09\u7b49\uff0c\u4e5f\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9032\u884c\u81ea\u8a02\u5be6\u73fe\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u7684add()\u904b\u7b97\u7b26\u53ef\u4ee5\u91cd\u8f09\uff0c\u53ef\u4ee5\u7528\u65bc\u5c0d\u4e0d\u540c\u985e\u578b\u7684\u6578\u64da\u9032\u884c\u52a0\u6cd5\u904b\u7b97\u3002\u672c\u6587\u5c07\u4ecb\u7d39add()\u904b\u7b97\u7b26\u7684\u91cd\u8f09\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u64cd\u4f5c\u4e0d\u540c\u985e\u578b\u7684\u6578\u64da\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-2900","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-KM","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2900","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=2900"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2900\/revisions"}],"predecessor-version":[{"id":2901,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2900\/revisions\/2901"}],"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=2900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}