{"id":2894,"date":"2025-05-28T17:09:34","date_gmt":"2025-06-04T09:28:34","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2894"},"modified":"2025-06-04T17:09:34","modified_gmt":"2025-06-04T09:28:34","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84repr%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%84repr%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 `repr()` \u51fd\u6578\uff1a\u7528\u65bc\u8abf\u8a66\u8207\u5c0d\u8c61\u8868\u793a\u7684\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, repr(), Python \u7a0b\u5f0f\u6559\u5b78, Python \u8abf\u8a66\"><\/p>\n<p>Python \u7684 `repr()` \u51fd\u6578\u662f\u4e00\u500b\u975e\u5e38\u5be6\u7528\u7684\u5de5\u5177\uff0c\u4e3b\u8981\u7528\u65bc\u8fd4\u56de\u5c0d\u8c61\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u7279\u5225\u5728\u8abf\u8a66\u904e\u7a0b\u4e2d\u76f8\u7576\u6709\u5e6b\u52a9\u3002\u5b83\u8b93\u6211\u5011\u80fd\u5920\u6aa2\u8996\u5c0d\u8c61\u7684\u5167\u90e8\u7d50\u69cb\u53ca\u5176\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u5f9e\u800c\u63d0\u9ad8\u7a0b\u5f0f\u7684\u53ef\u8b80\u6027\u548c\u53ef\u7dad\u8b77\u6027\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f `repr()` \u51fd\u6578\uff1f<\/h2>\n<p>`repr()` \u51fd\u6578\u662f Python \u4e2d\u7684\u4e00\u500b\u5167\u7f6e\u51fd\u6578\uff0c\u65e8\u5728\u8fd4\u56de\u5c0d\u8c61\u7684\u5b98\u65b9\u5b57\u7b26\u4e32\u8868\u793a\u3002\u9019\u500b\u5b57\u7b26\u4e32\u8868\u793a\u53ef\u4ee5\u7528\u65bc\u751f\u6210\u5c0d\u8c61\u7684\u63cf\u8ff0\uff0c\u901a\u5e38\u7528\u65bc\u958b\u767c\u548c\u8abf\u8a66\u904e\u7a0b\u4e2d\uff0c\u4ee5\u4fbf\u958b\u767c\u8005\u80fd\u5920\u5feb\u901f\u77ad\u89e3\u5c0d\u8c61\u7684\u72c0\u614b\u3002<\/p>\n<h2> `repr()` \u51fd\u6578\u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<p>\u4f7f\u7528 `repr()` \u51fd\u6578\u5341\u5206\u7c21\u55ae\uff0c\u53ea\u9700\u5c07\u5c0d\u8c61\u4f5c\u70ba\u53c3\u6578\u50b3\u905e\u7d66\u5b83\uff0c\u4fbf\u6703\u8fd4\u56de\u8a72\u5c0d\u8c61\u7684\u5b57\u7b26\u4e32\u8868\u793a\u3002\u4f8b\u5982\uff0c\u5c0d\u65bc\u5b57\u7b26\u4e32\u5c0d\u8c61\u7684\u4f7f\u7528\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_string = &#8216;Hello World!&#8217;<br \/>\nprint(repr(my_string))<br \/>\n&#8220;`<\/p>\n<p>\u8f38\u51fa\u7d50\u679c\u70ba\uff1a<\/p>\n<p>&#8220;`<br \/>\n&#8216;Hello World!&#8217;<br \/>\n&#8220;`<\/p>\n<p>\u9019\u500b\u8f38\u51fa\u986f\u793a\u4e86\u5b57\u7b26\u4e32\u7684\u539f\u59cb\u683c\u5f0f\uff0c\u5305\u62ec\u5f15\u865f\uff0c\u9019\u5c0d\u65bc\u8abf\u8a66\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<h2> `repr()` \u51fd\u6578\u7684\u61c9\u7528<\/h2>\n<p>`repr()` \u51fd\u6578\u4e0d\u50c5\u80fd\u5920\u986f\u793a\u5b57\u7b26\u4e32\u7684\u8868\u793a\uff0c\u9084\u53ef\u7528\u65bc\u5404\u985e\u5c0d\u8c61\uff0c\u4f8b\u5982\u5217\u8868\u548c\u5b57\u5178\u3002\u4ee5\u4e0b\u662f\u5c07\u4e00\u500b\u5217\u8868\u50b3\u905e\u7d66 `repr()` \u51fd\u6578\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmy_list = [1, 2, 3]\nprint(repr(my_list))<br \/>\n&#8220;`<\/p>\n<p>\u9019\u5c07\u8f38\u51fa\uff1a<\/p>\n<p>&#8220;`<br \/>\n[1, 2, 3]\n&#8220;`<\/p>\n<p>\u9019\u986f\u793a\u4e86\u5217\u8868\u7684\u5b8c\u6574\u7d50\u69cb\uff0c\u9019\u5728\u6aa2\u67e5\u6578\u64da\u6642\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u5c0d\u65bc\u81ea\u5b9a\u7fa9\u5c0d\u8c61\uff0c\u82e5\u60f3\u8981\u81ea\u5b9a\u7fa9 `repr()` \u7684\u8f38\u51fa\uff0c\u53ef\u4ee5\u5728\u985e\u4e2d\u5be6\u73fe `__repr__()` \u65b9\u6cd5\u3002\u4f8b\u5982\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 __repr__(self):<br \/>\n        return f&#8221;Person(name={self.name}, age={self.age})&#8221;<\/p>\n<p>person = Person(&#8220;Alice&#8221;, 30)<br \/>\nprint(repr(person))<br \/>\n&#8220;`<\/p>\n<p>\u8f38\u51fa\u5c07\u662f\uff1a<\/p>\n<p>&#8220;`<br \/>\nPerson(name=Alice, age=30)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u53ef\u4ee5\u4f7f\u81ea\u5b9a\u7fa9\u5c0d\u8c61\u7684\u8f38\u51fa\u66f4\u5177\u53ef\u8b80\u6027\uff0c\u5c0d\u65bc\u8abf\u8a66\u975e\u5e38\u6709\u5e6b\u52a9\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528 `repr()` \u6642\uff0c\u82e5\u767c\u73fe\u8f38\u51fa\u4e0d\u7b26\u5408\u9810\u671f\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<br \/>\n&#8211; \u78ba\u4fdd\u5c0d\u8c61\u7684 `__repr__()` \u65b9\u6cd5\u5df2\u6b63\u78ba\u5be6\u73fe\uff08\u5c0d\u65bc\u81ea\u5b9a\u7fa9\u985e\uff09\u3002<br \/>\n&#8211; \u78ba\u8a8d\u5c0d\u8c61\u6c92\u6709\u88ab\u610f\u5916\u4fee\u6539\uff0c\u5c0e\u81f4\u8f38\u51fa\u7570\u5e38\u3002<\/p>\n<p>\u4f7f\u7528 `repr()` \u7684\u6700\u4f73\u5be6\u8e10\u662f\uff0c\u5c0d\u65bc\u6240\u6709\u9700\u8981\u8abf\u8a66\u7684\u5c0d\u8c61\uff0c\u61c9\u8a72\u4f7f\u7528 `repr()` \u4f86\u751f\u6210\u5176\u8868\u793a\uff0c\u800c\u4e0d\u662f\u7c21\u55ae\u5730\u4f7f\u7528 `print()`\u3002<\/p>\n<p>\u82e5\u60f3\u8981\u9032\u4e00\u6b65\u5b78\u7fd2 Python \u7684\u5176\u4ed6\u6280\u5de7\uff0c\u6b61\u8fce\u53c3\u8003\u9019\u7bc7 [Python \u6559\u5b78\u6587](https:\/\/vocus.cc)\uff0c\u63d0\u4f9b\u66f4\u591a\u6df1\u5165\u7684\u5167\u5bb9\u8207\u793a\u4f8b\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Python \u7684 `repr()` \u51fd\u6578\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u4e0d\u50c5\u80fd\u5920\u8fd4\u56de\u985e\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u9084\u80fd\u5920\u5e6b\u52a9\u958b\u767c\u8005\u5728\u8abf\u8a66\u904e\u7a0b\u4e2d\u77ad\u89e3\u5c0d\u8c61\u7684\u7d50\u69cb\u3002\u7121\u8ad6\u662f\u5167\u5efa\u5c0d\u8c61\u9084\u662f\u81ea\u5b9a\u7fa9\u985e\uff0c`repr()` \u90fd\u80fd\u63d0\u4f9b\u6e05\u6670\u7684\u8868\u793a\uff0c\u63d0\u5347\u7a0b\u5f0f\u7684\u53ef\u8b80\u6027\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\u4e0d\u540c\uff1f**<br \/>\nA1: `repr()` \u65e8\u5728\u751f\u6210\u5c0d\u8c61\u7684\u6b63\u5f0f\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u901a\u5e38\u7528\u65bc\u8abf\u8a66\uff1b\u800c `str()` \u66f4\u52a0\u95dc\u6ce8\u5c0d\u8c61\u7684\u53ef\u8b80\u6027\uff0c\u901a\u5e38\u7528\u65bc\u7528\u6236\u5c55\u793a\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u81ea\u5b9a\u7fa9\u4e00\u500b\u985e\u7684 `repr()` \u65b9\u6cd5\uff1f**<br \/>\nA2: \u53ef\u4ee5\u5728\u985e\u4e2d\u5be6\u73fe `__repr__()` \u65b9\u6cd5\uff0c\u8a72\u65b9\u6cd5\u61c9\u8fd4\u56de\u4e00\u500b\u5b57\u7b26\u4e32\uff0c\u63cf\u8ff0\u5c0d\u8c61\u7684\u72c0\u614b\u3002<\/p>\n<p>**Q3: `repr()` \u51fd\u6578\u53ef\u4ee5\u7528\u65bc\u54ea\u4e9b\u985e\u578b\u7684\u5c0d\u8c61\uff1f**<br \/>\nA3: `repr()` \u53ef\u4ee5\u7528\u65bc\u6240\u6709 Python \u5c0d\u8c61\uff0c\u5305\u62ec\u5167\u5efa\u985e\u578b\u548c\u7528\u6236\u81ea\u5b9a\u7fa9\u985e\u578b\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u7372\u53d6\u5c0d\u8c61\u7684\u8a73\u7d30\u4fe1\u606f\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684repr()\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u8fd4\u56de\u985e\u7684\u5b57\u7b26\u4e32\u8868\u793a\uff0c\u901a\u5e38\u7528\u65bc\u8c03\u8bd5\u3002\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528repr()\u51fd\u6578\uff0c\u4ee5\u53ca\u5b83\u7684\u512a\u9ede\u548c\u7f3a\u9ede\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u638c\u63e1Python\u7a0b\u5e8f\u8a2d\u8a08\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-2894","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-KG","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2894","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=2894"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2894\/revisions"}],"predecessor-version":[{"id":2895,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2894\/revisions\/2895"}],"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=2894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}