{"id":2904,"date":"2025-05-28T17:27:00","date_gmt":"2025-06-04T09:26:59","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2904"},"modified":"2025-06-04T17:27:00","modified_gmt":"2025-06-04T09:26:59","slug":"python%e4%b8%ad%e7%9a%84mul%e5%87%bd%e6%95%b8%ef%bc%9a%e9%87%8d%e8%bc%89%e9%a1%9e%e7%9a%84%e4%b9%98%e6%b3%95%e9%81%8b%e7%ae%97%e7%ac%a6","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/python%e4%b8%ad%e7%9a%84mul%e5%87%bd%e6%95%b8%ef%bc%9a%e9%87%8d%e8%bc%89%e9%a1%9e%e7%9a%84%e4%b9%98%e6%b3%95%e9%81%8b%e7%ae%97%e7%ac%a6\/","title":{"rendered":"\u5168\u9762\u89e3\u6790 Python \u4e2d\u7684 `mul()` \u51fd\u6578\uff1a\u91cd\u8f09\u985e\u7684\u4e58\u6cd5\u904b\u7b97\u7b26"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, mul(), \u91cd\u8f09\u904b\u7b97\u7b26, Python \u6559\u5b78\"><\/p>\n<p>\u5728 Python \u4e2d\uff0c`mul()` \u51fd\u6578\u662f\u7528\u65bc\u91cd\u8f09\u985e\u7684\u4e58\u6cd5\u904b\u7b97\u7b26\u7684\u5f37\u5927\u5de5\u5177\u3002\u900f\u904e\u9019\u500b\u51fd\u6578\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u81ea\u5b9a\u7fa9\u5c0d\u7279\u5b9a\u985e\u578b\u7684\u4e58\u6cd5\u904b\u7b97\uff0c\u5f9e\u800c\u589e\u5f37\u985e\u7684\u8868\u73fe\u529b\u8207\u9748\u6d3b\u6027\u3002\u4ee5\u4e0b\u662f 2025 \u5e74\u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u7684\u8a73\u7d30\u8aaa\u660e\u53ca\u5be6\u4f5c\u7bc4\u4f8b\u3002<\/p>\n<p>### `mul()` \u51fd\u6578\u7684\u8a9e\u6cd5\u8207\u53c3\u6578<\/p>\n<p>`mul()` \u51fd\u6578\u7684\u57fa\u672c\u8a9e\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\ndef mul(self, other):<br \/>\n    # code<br \/>\n&#8220;`<\/p>\n<p>&#8211; **self**\uff1a\u7576\u524d\u5c0d\u8c61\u7684\u5f15\u7528\u3002<br \/>\n&#8211; **other**\uff1a\u53e6\u4e00\u500b\u5c0d\u8c61\u7684\u5f15\u7528\u3002<\/p>\n<p>### `mul()` \u51fd\u6578\u7684\u5be6\u4f5c\u7bc4\u4f8b<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u5b8c\u6574\u7684\u793a\u7bc4\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728\u81ea\u5b9a\u7fa9\u985e\u4e2d\u5be6\u4f5c `mul()` \u51fd\u6578\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass A:<br \/>\n    def __init__(self, value):<br \/>\n        self.value = value<\/p>\n<p>    def __mul__(self, other):<br \/>\n        if isinstance(other, A):<br \/>\n            return A(self.value * other.value)<br \/>\n        return NotImplemented<\/p>\n<p>    def __repr__(self):<br \/>\n        return f&#8221;A({self.value})&#8221;<\/p>\n<p># \u4f7f\u7528\u7bc4\u4f8b<br \/>\na = A(2)<br \/>\nb = A(3)<br \/>\nc = a * b  # \u4f7f\u7528\u91cd\u8f09\u904b\u7b97\u7b26<br \/>\nprint(c)  # \u8f38\u51fa\uff1aA(6)<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u6211\u5011\u5b9a\u7fa9\u4e86\u4e00\u500b `A` \u985e\uff0c\u4e26\u91cd\u8f09\u4e86 `__mul__()` \u65b9\u6cd5\u3002\u7576\u5169\u500b `A` \u985e\u7684\u5be6\u4f8b\u9032\u884c\u4e58\u6cd5\u904b\u7b97\u6642\uff0c\u6703\u8fd4\u56de\u4e00\u500b\u65b0\u7684 `A` \u5be6\u4f8b\uff0c\u5176\u503c\u70ba\u5169\u500b\u5be6\u4f8b\u503c\u7684\u4e58\u7a4d\u3002<\/p>\n<p>### \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u7576\u4f7f\u7528 `mul()` \u51fd\u6578\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u932f\u8aa4\uff1a<\/p>\n<p>&#8211; **TypeError**\uff1a\u7576\u4f60\u5617\u8a66\u5c07\u4e0d\u76f8\u5bb9\u7684\u985e\u578b\u9032\u884c\u4e58\u6cd5\u904b\u7b97\u6642\uff0c\u6703\u5f15\u767c\u6b64\u932f\u8aa4\u3002\u78ba\u4fdd\u5728 `mul()` \u51fd\u6578\u4e2d\u6aa2\u67e5 `other` \u7684\u985e\u578b\uff0c\u4e26\u8fd4\u56de `NotImplemented`\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u4e58\u6cd5\u904b\u7b97\u5916\uff0c`mul()` \u51fd\u6578\u9084\u53ef\u4ee5\u7528\u65bc\u5be6\u4f5c\u66f4\u8907\u96dc\u7684\u6578\u5b78\u904b\u7b97\uff0c\u4f8b\u5982\u77e9\u9663\u76f8\u4e58\u6216\u5411\u91cf\u904b\u7b97\u3002\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u64f4\u5c55\u6b64\u529f\u80fd\uff0c\u70ba\u985e\u578b\u589e\u6dfb\u66f4\u591a\u6578\u5b78\u64cd\u4f5c\u3002<\/p>\n<p>### \u7d50\u8ad6<\/p>\n<p>\u638c\u63e1 Python \u4e2d\u7684 `mul()` \u51fd\u6578\u53ef\u4ee5\u5927\u5e45\u63d0\u5347\u5c0d\u81ea\u5b9a\u7fa9\u985e\u7684\u64cd\u4f5c\u9748\u6d3b\u6027\u3002\u96a8\u8457 Python \u8a9e\u8a00\u7684\u6f14\u9032\uff0c\u958b\u767c\u8005\u61c9\u96a8\u6642\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8b58\uff0c\u4ee5\u4fbf\u4f7f\u7528\u6700\u65b0\u7684\u8a9e\u6cd5\u548c\u6700\u4f73\u5be6\u8e10\u3002\u66f4\u591a Python \u6559\u5b78\uff0c\u8acb\u53c3\u8003 [\u9019\u88e1](https:\/\/vocus.cc\/article\/5d9e2f2a4c3c4f0018a518b9)\u3002<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: `mul()` \u51fd\u6578\u7684\u8fd4\u56de\u503c\u662f\u4ec0\u9ebc\uff1f**<br \/>\nA1: `mul()` \u51fd\u6578\u901a\u5e38\u8fd4\u56de\u4e00\u500b\u65b0\u7684\u5c0d\u8c61\uff0c\u8a72\u5c0d\u8c61\u7684\u503c\u662f\u5169\u500b\u64cd\u4f5c\u6578\u7684\u4e58\u7a4d\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u907f\u514d `TypeError`\uff1f**<br \/>\nA2: \u5728 `mul()` \u51fd\u6578\u4e2d\u6aa2\u67e5 `other` \u7684\u985e\u578b\uff0c\u4e26\u5728\u4e0d\u76f8\u5bb9\u6642\u8fd4\u56de `NotImplemented`\u3002<\/p>\n<p>**Q3: `mul()` \u51fd\u6578\u53ef\u4ee5\u7528\u65bc\u54ea\u4e9b\u8cc7\u6599\u985e\u578b\uff1f**<br \/>\nA3: `mul()` \u51fd\u6578\u53ef\u7528\u65bc\u4efb\u4f55\u81ea\u5b9a\u7fa9\u985e\u578b\uff0c\u53ea\u8981\u4f60\u5b9a\u7fa9\u4e86\u76f8\u61c9\u7684\u91cd\u8f09\u904b\u7b97\u7b26\u3002<\/p>\n<p>&#8212; <\/p>\n<p>\u4ee5\u4e0a\u662f\u91dd\u5c0d\u8a72\u6587\u7ae0\u7684\u5168\u9762\u512a\u5316\uff0c\u63d0\u5347\u4e86\u5167\u5bb9\u7684\u5be6\u7528\u6027\u8207 SEO \u6548\u679c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python\u4e2d\u7684mul()\u51fd\u6578\u662f\u4e00\u7a2e\u91cd\u8f09\u985e\u7684\u4e58\u6cd5\u904b\u7b97\u7b26\uff0c\u53ef\u4ee5\u7528\u65bc\u5c0d\u5169\u500b\u5c0d\u8c61\u9032\u884c\u4e58\u6cd5\u904b\u7b97\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u65b0\u7684\u5c0d\u8c61\u3002\u5b83\u53ef\u4ee5\u7528\u65bc\u69cb\u5efa\u66f4\u9ad8\u7d1a\u7684\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5217\u8868\u3001\u5b57\u5178\u548c\u5143\u7d44\uff0c\u4e26\u63d0\u4f9b\u66f4\u591a\u7684\u63a7\u5236\u548c\u7075\u6d3b\u6027\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-2904","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-KQ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2904","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=2904"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2904\/revisions"}],"predecessor-version":[{"id":2905,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2904\/revisions\/2905"}],"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=2904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}