{"id":3154,"date":"2025-06-03T15:51:25","date_gmt":"2025-06-04T08:46:24","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3154"},"modified":"2025-06-04T15:51:25","modified_gmt":"2025-06-04T08:46:24","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84repr%e5%87%bd%e6%95%b8-3","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84repr%e5%87%bd%e6%95%b8-3\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 repr() \u51fd\u6578\uff1a\u7528\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff082025 \u6700\u65b0\u7248\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, repr(), Python \u6559\u5b78, Python \u51fd\u6578\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 repr() \u51fd\u6578<\/h1>\n<p>Python \u7684 `repr()` \u51fd\u6578\u662f\u7528\u4f86\u53d6\u5f97\u5c0d\u8c61\u7684\u5b57\u4e32\u8868\u793a\u7684\u51fd\u6578\u3002\u9019\u500b\u51fd\u6578\u4e0d\u50c5\u53ef\u4ee5\u7528\u4f86\u7372\u53d6\u5c0d\u8c61\u7684\u5b57\u4e32\u8868\u793a\uff0c\u9084\u53ef\u4ee5\u5c0d\u5c0d\u8c61\u9032\u884c\u8a73\u7d30\u7684\u6aa2\u67e5\u3002`repr()` \u51fd\u6578\u7684\u8a2d\u8a08\u76ee\u7684\u662f\u70ba\u4e86\u751f\u6210\u53ef\u4ee5\u7528\u65bc\u91cd\u65b0\u5275\u5efa\u8a72\u5c0d\u8c61\u7684\u5b57\u4e32\uff0c\u9019\u5c0d\u65bc\u8abf\u8a66\u548c\u958b\u767c\u904e\u7a0b\u4e2d\u975e\u5e38\u6709\u5e6b\u52a9\u3002<\/p>\n<h2>repr() \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>`repr()` \u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u8981\u5c07\u5c0d\u8c61\u4f5c\u70ba\u53c3\u6578\u50b3\u905e\u7d66\u5b83\uff0c\u5b83\u5c31\u6703\u8fd4\u56de\u5c0d\u8c61\u7684\u5b57\u4e32\u8868\u793a\u3002\u4ee5\u4e0b\u662f\u5e7e\u500b\u57fa\u672c\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nx = 10<br \/>\nprint(repr(x))<br \/>\n&#8220;`<\/p>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u5c07\u8f38\u51fa\uff1a<\/p>\n<p>&#8220;`<br \/>\n10<br \/>\n&#8220;`<\/p>\n<p>\u9664\u4e86\u57fa\u672c\u6578\u64da\u985e\u578b\uff0c`repr()` \u51fd\u6578\u9084\u53ef\u4ee5\u7528\u65bc\u8907\u96dc\u7684\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5217\u8868\u3001\u5b57\u5178\u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nx = [1, 2, 3]\nprint(repr(x))<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u7684\u8f38\u51fa\u5c07\u662f\uff1a<\/p>\n<p>&#8220;`<br \/>\n[1, 2, 3]\n&#8220;`<\/p>\n<h2>\u4f7f\u7528 repr() \u9032\u884c\u8a73\u7d30\u6aa2\u67e5<\/h2>\n<p>`repr()` \u51fd\u6578\u7684\u53e6\u4e00\u500b\u5f37\u5927\u529f\u80fd\u662f\u5b83\u53ef\u4ee5\u5e6b\u52a9\u6211\u5011\u9032\u884c\u5c0d\u8c61\u7684\u8a73\u7d30\u6aa2\u67e5\u3002\u9019\u5728\u8abf\u8a66\u904e\u7a0b\u4e2d\u5c24\u5176\u6709\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528 `repr()` \u6aa2\u67e5\u81ea\u5b9a\u7fa9\u5c0d\u8c61\uff1a<\/p>\n<p>&#8220;`python<br \/>\nclass MyClass:<br \/>\n    def __init__(self, value):<br \/>\n        self.value = value<\/p>\n<p>    def __repr__(self):<br \/>\n        return f&#8221;MyClass(value={self.value})&#8221;<\/p>\n<p>obj = MyClass(10)<br \/>\nprint(repr(obj))<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u4ee3\u78bc\u5c07\u8f38\u51fa\uff1a<\/p>\n<p>&#8220;`<br \/>\nMyClass(value=10)<br \/>\n&#8220;`<\/p>\n<p>\u900f\u904e\u8986\u84cb `__repr__` \u65b9\u6cd5\uff0c\u6211\u5011\u53ef\u4ee5\u63a7\u5236 `repr()` \u51fd\u6578\u7684\u8f38\u51fa\uff0c\u9019\u6a23\u5728\u8abf\u8a66\u6642\u53ef\u4ee5\u66f4\u6e05\u695a\u5730\u4e86\u89e3\u5c0d\u8c61\u7684\u72c0\u614b\u3002<\/p>\n<h2>\u5e38\u898b\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5728\u4f7f\u7528 `repr()` \u51fd\u6578\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u7684\u554f\u984c\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>1. **\u81ea\u5b9a\u7fa9\u985e\u6c92\u6709\u5be6\u73fe `__repr__` \u65b9\u6cd5**\uff1a\u5982\u679c\u4f60\u7684\u81ea\u5b9a\u7fa9\u985e\u6c92\u6709\u5be6\u73fe `__repr__`\uff0c\u5247\u5c07\u4f7f\u7528\u9810\u8a2d\u7684\u884c\u70ba\uff0c\u9019\u53ef\u80fd\u4e0d\u6703\u63d0\u4f9b\u6709\u7528\u7684\u4fe1\u606f\u3002\u5efa\u8b70\u59cb\u7d42\u70ba\u81ea\u5b9a\u7fa9\u985e\u5be6\u73fe `__repr__` \u65b9\u6cd5\u3002<\/p>\n<p>2. **\u8f38\u51fa\u4e0d\u6613\u8b80**\uff1a\u5982\u679c `repr()` \u7684\u8f38\u51fa\u4e0d\u6613\u8b80\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 `__str__` \u65b9\u6cd5\u4f86\u63d0\u4f9b\u66f4\u53cb\u597d\u7684\u8868\u793a\uff0c\u4e26\u78ba\u4fdd `__repr__` \u8fd4\u56de\u7684\u5b57\u4e32\u662f\u53ef\u4ee5\u7528\u4f86\u91cd\u65b0\u5275\u5efa\u5c0d\u8c61\u7684\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u9664\u4e86\u57fa\u672c\u7528\u6cd5\uff0c`repr()` \u51fd\u6578\u9084\u53ef\u4ee5\u8207\u5176\u4ed6 Python \u529f\u80fd\u7d50\u5408\u4f7f\u7528\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8211; **\u5728\u65e5\u8a8c\u4e2d\u8a18\u9304\u5c0d\u8c61**\uff1a\u4f7f\u7528 `repr()` \u751f\u6210\u5c0d\u8c61\u7684\u5b57\u4e32\u8868\u793a\uff0c\u4e26\u5c07\u5176\u5beb\u5165\u65e5\u8a8c\uff0c\u9019\u6a23\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8ffd\u8e64\u61c9\u7528\u7684\u72c0\u614b\u3002<br \/>\n&#8211; **\u8abf\u8a66\u5de5\u5177**\uff1a\u8a31\u591a\u8abf\u8a66\u5de5\u5177\u548c IDE \u90fd\u6703\u4f7f\u7528 `repr()` \u4f86\u986f\u793a\u8b8a\u6578\u5167\u5bb9\uff0c\u9019\u4f7f\u5f97\u958b\u767c\u8005\u80fd\u5920\u5feb\u901f\u7372\u53d6\u5c0d\u8c61\u7684\u72c0\u614b\u3002<\/p>\n<p>\u5982\u679c\u60f3\u8981\u4e86\u89e3\u66f4\u591a Python \u51fd\u6578\u7684\u61c9\u7528\uff0c\u53ef\u4ee5\u53c3\u8003 [\u9019\u88e1](https:\/\/vocus.cc), \u7372\u5f97\u66f4\u591a\u5be6\u7528\u7684\u6559\u5b78\u8cc7\u6e90\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Python \u7684 `repr()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u7528\u4f86\u7372\u53d6\u5c0d\u8c61\u7684\u5b57\u4e32\u8868\u793a\u4e26\u9032\u884c\u8a73\u7d30\u6aa2\u67e5\u3002\u901a\u904e\u5be6\u65bd\u6700\u4f73\u5be6\u8e10\uff0c\u4f8b\u5982\u8986\u84cb `__repr__` \u65b9\u6cd5\uff0c\u6211\u5011\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u78bc\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002\u9019\u4e9b\u6280\u5de7\u4e0d\u50c5\u9069\u7528\u65bc\u65b0\u624b\uff0c\u4e5f\u5c0d\u7d93\u9a57\u8c50\u5bcc\u7684\u958b\u767c\u8005\u6709\u5e6b\u52a9\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: `repr()` \u548c `str()` \u6709\u4ec0\u9ebc\u5340\u5225\uff1f**<br \/>\nA1: `repr()` \u65e8\u5728\u751f\u6210\u53ef\u4ee5\u7528\u65bc\u91cd\u65b0\u5275\u5efa\u5c0d\u8c61\u7684\u5b57\u4e32\uff0c\u800c `str()` \u66f4\u52a0\u95dc\u6ce8\u4eba\u985e\u53ef\u8b80\u6027\u3002\u901a\u5e38\uff0c`repr()` \u7528\u65bc\u8abf\u8a66\uff0c`str()` \u7528\u65bc\u986f\u793a\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u5728\u81ea\u5b9a\u7fa9\u985e\u4e2d\u5be6\u73fe `repr()`\uff1f**<br \/>\nA2: \u4f60\u53ef\u4ee5\u901a\u904e\u5b9a\u7fa9 `__repr__` \u65b9\u6cd5\u4f86\u5be6\u73fe\uff0c\u8a72\u65b9\u6cd5\u61c9\u8fd4\u56de\u4e00\u500b\u5b57\u7b26\u4e32\uff0c\u8a72\u5b57\u7b26\u4e32\u63cf\u8ff0\u4e86\u5c0d\u8c61\u7684\u72c0\u614b\u3002<\/p>\n<p>**Q3: \u6709\u54ea\u4e9b\u60c5\u6cc1\u4e0b\u9700\u8981\u4f7f\u7528 `repr()`\uff1f**<br \/>\nA3: \u7576\u4f60\u9700\u8981\u8abf\u8a66\u4ee3\u78bc\uff0c\u6216\u60f3\u8981\u7372\u53d6\u5c0d\u8c61\u7684\u8a73\u7d30\u4fe1\u606f\u6642\uff0c\u4f7f\u7528 `repr()` \u662f\u975e\u5e38\u6709\u5e6b\u52a9\u7684\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684repr()\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u5c07\u4efb\u4f55Python\u5c0d\u8c61\u8f49\u63db\u70ba\u5b57\u7b26\u4e32\uff0c\u4e26\u63d0\u4f9b\u4e00\u7a2e\u53ef\u8b80\u6027\u5f37\u7684\u8868\u793a\u6cd5\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u4e86\u89e3Python\u5c0d\u8c61\u7684\u5167\u5bb9\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-3154","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-OS","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3154","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=3154"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3154\/revisions"}],"predecessor-version":[{"id":12923,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3154\/revisions\/12923"}],"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=3154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}