{"id":2902,"date":"2025-05-28T16:59:46","date_gmt":"2025-06-04T09:26:46","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2902"},"modified":"2025-06-04T16:59:46","modified_gmt":"2025-06-04T09:26:46","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84sub%e6%b8%9b%e6%b3%95%e9%81%8b%e7%ae%97%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84sub%e6%b8%9b%e6%b3%95%e9%81%8b%e7%ae%97%e7%ac%a6\/","title":{"rendered":"2025 \u6700\u65b0 Python \u7a0b\u5f0f\u6559\u5b78\uff1a\u6df1\u5165\u7406\u89e3 sub() \u6e1b\u6cd5\u904b\u7b97\u7b26\u7684\u91cd\u8f09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, sub(), \u6e1b\u6cd5\u904b\u7b97\u7b26, \u985e\u5225\u91cd\u8f09, Python \u6559\u5b78, Python 2025\"><\/p>\n<p>\u5728 Python \u4e2d\uff0c`sub()` \u51fd\u5f0f\u662f\u7528\u65bc\u91cd\u8f09\u985e\u7684\u6e1b\u6cd5\u904b\u7b97\u7b26\u3002\u9019\u500b\u529f\u80fd\u975e\u5e38\u5f37\u5927\uff0c\u5141\u8a31\u958b\u767c\u8005\u81ea\u5b9a\u7fa9\u6e1b\u6cd5\u904b\u7b97\u7b26\u7684\u884c\u70ba\uff0c\u5f9e\u800c\u4f7f\u5176\u9069\u7528\u65bc\u4efb\u4f55\u81ea\u5b9a\u7fa9\u7684\u985e\u5225\u3002\u9019\u7bc7\u6587\u7ae0\u5c07\u4ecb\u7d39\u5982\u4f55\u6b63\u78ba\u5730\u4f7f\u7528 `sub()` \u51fd\u5f0f\uff0c\u4e26\u63d0\u4f9b\u5b8c\u6574\u7684\u7bc4\u4f8b\u548c\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<p>## sub() \u51fd\u5f0f\u7684\u57fa\u672c\u8a9e\u6cd5<\/p>\n<p>`sub()` \u51fd\u5f0f\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\ndef __sub__(self, other):<br \/>\n    # \u81ea\u5b9a\u7fa9\u7684\u6e1b\u6cd5\u904b\u7b97\u7b26<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u88e1\uff0c`self` \u548c `other` \u5206\u5225\u4ee3\u8868\u88ab\u6e1b\u6578\u548c\u6e1b\u6578\u3002\u958b\u767c\u8005\u53ef\u4ee5\u81ea\u884c\u5b9a\u7fa9\u6e1b\u6cd5\u904b\u7b97\u7b26\u7684\u884c\u70ba\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\ndef __sub__(self, other):<br \/>\n    return self.value &#8211; other.value<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c07\u6e1b\u6cd5\u904b\u7b97\u7b26\u5b9a\u7fa9\u70ba\u5c07\u5169\u500b\u7269\u4ef6\u7684 `value` \u5c6c\u6027\u76f8\u6e1b\u3002<\/p>\n<p>## \u5be6\u4f5c\u7bc4\u4f8b<\/p>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u5c07\u901a\u904e\u4e00\u500b\u5be6\u4f5c\u7bc4\u4f8b\u4f86\u5c55\u793a\u5982\u4f55\u4f7f\u7528 `sub()` \u51fd\u5f0f\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyClass:<br \/>\n    def __init__(self, value):<br \/>\n        self.value = value<\/p>\n<p>    def __sub__(self, other):<br \/>\n        return self.value &#8211; other.value<\/p>\n<p># \u5275\u5efa\u5169\u500b MyClass \u7269\u4ef6<br \/>\na = MyClass(10)<br \/>\nb = MyClass(5)<\/p>\n<p># \u4f7f\u7528\u91cd\u8f09\u7684\u6e1b\u6cd5\u904b\u7b97\u7b26<br \/>\nc = a &#8211; b<br \/>\nprint(c)  # \u8f38\u51fa 5<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u7bc4\u4f8b\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b `MyClass` \u985e\uff0c\u4e26\u4e14\u5c07 `__sub__()` \u51fd\u5f0f\u5b9a\u7fa9\u70ba\u6e1b\u6cd5\u904b\u7b97\u7b26\u3002\u7136\u5f8c\uff0c\u6211\u5011\u5275\u5efa\u4e86\u5169\u500b `MyClass` \u7269\u4ef6\uff0c\u4e26\u4f7f\u7528 `-` \u904b\u7b97\u7b26\u76f8\u6e1b\uff0c\u6700\u7d42\u8f38\u51fa\u7d50\u679c\u70ba 5\u3002<\/p>\n<p>## \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528 `sub()` \u51fd\u5f0f\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u5e38\u898b\u932f\u8aa4\uff1a<\/p>\n<p>1. **TypeError**\uff1a\u5982\u679c `other` \u4e0d\u662f `MyClass` \u7684\u5be6\u4f8b\uff0c\u5247\u6703\u5f15\u767c `TypeError`\u3002\u70ba\u4e86\u907f\u514d\u9019\u500b\u554f\u984c\uff0c\u60a8\u53ef\u4ee5\u5728 `__sub__` \u51fd\u5f0f\u4e2d\u6dfb\u52a0\u985e\u578b\u6aa2\u67e5\uff1a<br \/>\n    &#8220;`python<br \/>\n    def __sub__(self, other):<br \/>\n        if not isinstance(other, MyClass):<br \/>\n            raise TypeError(&#8220;Unsupported type for subtraction&#8221;)<br \/>\n        return self.value &#8211; other.value<br \/>\n    &#8220;`<\/p>\n<p>2. **AttributeError**\uff1a\u78ba\u4fdd `self` \u548c `other` \u90fd\u6709 `value` \u5c6c\u6027\u3002\u53ef\u8003\u616e\u4f7f\u7528 `hasattr()` \u9032\u884c\u6aa2\u67e5\u3002<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u91cd\u8f09\u904b\u7b97\u7b26\u4e0d\u50c5\u9650\u65bc\u6e1b\u6cd5\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u5176\u4ed6\u904b\u7b97\u7b26\uff0c\u5982\u52a0\u6cd5\uff08`__add__`\uff09\u3001\u4e58\u6cd5\uff08`__mul__`\uff09\u7b49\u3002\u9019\u4f7f\u5f97\u81ea\u5b9a\u7fa9\u985e\u5225\u7684\u884c\u70ba\u66f4\u52a0\u9748\u6d3b\u548c\u5f37\u5927\u3002<\/p>\n<p>\u9032\u4e00\u6b65\u4e86\u89e3 Python \u7684\u904b\u7b97\u7b26\u91cd\u8f09\uff0c\u60a8\u53ef\u4ee5\u53c3\u8003 [\u9019\u7bc7\u5b8c\u6574\u7684\u6559\u5b78\u6587\u7ae0](https:\/\/vocus.cc\/article\/12345678) \u4ee5\u7372\u5f97\u66f4\u591a\u8cc7\u8a0a\u3002<\/p>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: Python \u4e2d\u904b\u7b97\u7b26\u91cd\u8f09\u662f\u4ec0\u9ebc\uff1f**<br \/>\nA1: \u904b\u7b97\u7b26\u91cd\u8f09\u662f\u4e00\u7a2e\u529f\u80fd\uff0c\u5141\u8a31\u958b\u767c\u8005\u70ba\u81ea\u5b9a\u7fa9\u985e\u5225\u5b9a\u7fa9\u904b\u7b97\u7b26\u7684\u884c\u70ba\uff0c\u4f7f\u5176\u80fd\u5920\u50cf\u5167\u5efa\u985e\u5225\u4e00\u6a23\u9032\u884c\u904b\u7b97\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u78ba\u4fdd\u91cd\u8f09\u904b\u7b97\u7b26\u7684\u5b89\u5168\u6027\uff1f**<br \/>\nA2: \u5728\u91cd\u8f09\u904b\u7b97\u7b26\u6642\uff0c\u61c9\u52a0\u5165\u985e\u578b\u6aa2\u67e5\u4ee5\u78ba\u4fdd\u53c3\u6578\u7684\u6709\u6548\u6027\uff0c\u4e26\u8655\u7406\u53ef\u80fd\u7684\u932f\u8aa4\uff0c\u5982 `TypeError` \u548c `AttributeError`\u3002<\/p>\n<p>**Q3: \u9664\u4e86\u6e1b\u6cd5\uff0c\u9084\u53ef\u4ee5\u91cd\u8f09\u54ea\u4e9b\u904b\u7b97\u7b26\uff1f**<br \/>\nA3: Python \u652f\u6301\u591a\u7a2e\u904b\u7b97\u7b26\u91cd\u8f09\uff0c\u5982\u52a0\u6cd5\uff08`__add__`\uff09\u3001\u4e58\u6cd5\uff08`__mul__`\uff09\u3001\u9664\u6cd5\uff08`__truediv__`\uff09\u7b49\uff0c\u9019\u4e9b\u90fd\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u9032\u884c\u81ea\u5b9a\u7fa9\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2Python\u4e2d\u7684sub()\u6e1b\u6cd5\u904b\u7b97\u7b26\uff0c\u4e86\u89e3\u5b83\u7684\u529f\u80fd\u548c\u7528\u6cd5\uff0c\u4ee5\u53ca\u5b83\u5982\u4f55\u7528\u65bc\u91cd\u8f09\u985e\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0ePython\u4e2d\u7684sub()\u6e1b\u6cd5\u904b\u7b97\u7b26\uff0c\u8b93\u4f60\u66f4\u52a0\u4e86\u89e3\u5b83\u7684\u529f\u80fd\u548c\u7528\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-2902","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-KO","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2902","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=2902"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2902\/revisions"}],"predecessor-version":[{"id":2903,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2902\/revisions\/2903"}],"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=2902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}