{"id":3284,"date":"2025-05-29T16:20:39","date_gmt":"2025-06-04T08:21:38","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3284"},"modified":"2025-06-04T16:20:39","modified_gmt":"2025-06-04T08:21:38","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84repr%e5%87%bd%e6%95%b8-4","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-4\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 repr() \u51fd\u6578\uff1a\u7528\u6cd5\u3001\u512a\u9ede\u8207\u6700\u4f73\u5be6\u8e10\uff082025 \u6700\u65b0\u7248\u672c\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>\u5728 Python \u4e2d\uff0c`repr()` \u51fd\u6578\u662f\u4e00\u500b\u6975\u5176\u91cd\u8981\u7684\u5de5\u5177\uff0c\u7528\u65bc\u7372\u53d6\u5c0d\u8c61\u7684\u5b57\u4e32\u8868\u793a\u3002\u9019\u4e0d\u50c5\u80fd\u5e6b\u52a9\u6211\u5011\u5728\u8abf\u8a66\u6642\u8a73\u7d30\u6aa2\u67e5\u5c0d\u8c61\u7684\u5167\u5bb9\uff0c\u9084\u80fd\u5728\u9700\u8981\u6642\u5c07\u5c0d\u8c61\u5e8f\u5217\u5316\u70ba\u53ef\u5b58\u5132\u7684\u5b57\u4e32\u683c\u5f0f\u3002<\/p>\n<p>\u9019\u7bc7\u6587\u7ae0\u5c07\u4ecb\u7d39 `repr()` \u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5\u3001\u512a\u9ede\u53ca\u5176\u6700\u4f73\u5be6\u8e10\uff0c\u4e26\u63d0\u4f9b\u5be6\u4f5c\u7bc4\u4f8b\uff0c\u5e6b\u52a9\u4f60\u66f4\u6df1\u5165\u7406\u89e3\u9019\u500b\u51fd\u6578\u7684\u61c9\u7528\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f repr() \u51fd\u6578\uff1f<\/h2>\n<p>`repr()` \u51fd\u6578\u7684\u4e3b\u8981\u76ee\u7684\u662f\u8fd4\u56de\u4e00\u500b\u5c0d\u8c61\u7684\u53ef\u6253\u5370\u8868\u793a\uff0c\u9019\u5c0d\u65bc\u958b\u767c\u8005\u5728\u8abf\u8a66\u6642\u975e\u5e38\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5b83\u80fd\u63d0\u4f9b\u5c0d\u8c61\u7684\u8a73\u7d30\u8cc7\u8a0a\uff0c\u5e6b\u52a9\u6211\u5011\u7406\u89e3\u5176\u7d50\u69cb\u548c\u5167\u5bb9\u3002\u5728 Python \u4e2d\uff0c\u9019\u500b\u51fd\u6578\u5e38\u7528\u65bc\u8abf\u8a66\u548c\u65e5\u8a8c\u8a18\u9304\u3002<\/p>\n<h2>repr() \u51fd\u6578\u7684\u4f7f\u7528\u65b9\u6cd5<\/h2>\n<p>\u4f7f\u7528 `repr()` \u51fd\u6578\u975e\u5e38\u7c21\u55ae\u3002\u53ea\u9700\u8981\u5c07\u5e0c\u671b\u7372\u53d6\u8868\u793a\u7684\u5c0d\u8c61\u4f5c\u70ba\u53c3\u6578\u50b3\u905e\u7d66\u8a72\u51fd\u6578\u3002\u4f8b\u5982\uff1a<\/p>\n<p>&#8220;`python<br \/>\nx = &#8220;Hello World&#8221;<br \/>\nprint(repr(x))<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u7d50\u679c\u5982\u4e0b\uff1a<\/p>\n<p>&#8220;`<br \/>\n&#8216;Hello World&#8217;<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c`repr()` \u51fd\u6578\u5c07\u5b57\u4e32\u5c0d\u8c61\u8f49\u63db\u70ba\u5b57\u4e32\u8868\u793a\uff0c\u4e26\u5c07\u5b57\u4e32\u4e2d\u7684\u7279\u6b8a\u5b57\u5143\u8f49\u63db\u70ba\u53ef\u8b80\u7684\u5b57\u5143\u3002<\/p>\n<h2>repr() \u51fd\u6578\u7684\u512a\u9ede<\/h2>\n<p>\u4f7f\u7528 `repr()` \u51fd\u6578\u6709\u591a\u500b\u512a\u9ede\uff1a<\/p>\n<p>1. **\u8a73\u7d30\u6aa2\u67e5\u5c0d\u8c61**\uff1a\u4f60\u53ef\u4ee5\u4f7f\u7528 `repr()` \u4f86\u6aa2\u67e5\u5c0d\u8c61\u7684\u5c6c\u6027\u548c\u65b9\u6cd5\uff0c\u9019\u5c0d\u65bc\u8abf\u8a66\u662f\u76f8\u7576\u6709\u5e6b\u52a9\u7684\u3002<\/p>\n<p>2. **\u5e8f\u5217\u5316\u5c0d\u8c61**\uff1a\u5b83\u80fd\u5c07\u5c0d\u8c61\u8f49\u63db\u70ba\u5b57\u4e32\uff0c\u4ee5\u4fbf\u65bc\u5c0d\u8c61\u7684\u5b58\u5132\u548c\u50b3\u8f38\uff0c\u9019\u5c0d\u65bc\u9700\u8981\u5c07\u6578\u64da\u5beb\u5165\u6587\u4ef6\u6216\u7db2\u7d61\u50b3\u8f38\u7684\u5834\u5408\u7279\u5225\u91cd\u8981\u3002<\/p>\n<p>3. **\u9069\u7528\u65bc\u81ea\u5b9a\u7fa9\u5c0d\u8c61**\uff1a\u7576\u4f60\u70ba\u81ea\u5b9a\u7fa9\u985e\u5225\u5be6\u73fe `__repr__` \u65b9\u6cd5\u6642\uff0c\u53ef\u4ee5\u4f7f `repr()` \u8fd4\u56de\u66f4\u5177\u610f\u7fa9\u7684\u8868\u793a\u3002\u9019\u6a23\u5728\u8abf\u8a66\u6642\u5c31\u80fd\u66f4\u76f4\u89c0\u5730\u4e86\u89e3\u5c0d\u8c61\u7684\u72c0\u614b\u3002<\/p>\n<h2>\u5be6\u4f5c\u7bc4\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u793a\u7bc4\u5982\u4f55\u5728\u81ea\u5b9a\u7fa9\u985e\u4e2d\u4f7f\u7528 `repr()`\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!r}, age={self.age!r})&#8221;<\/p>\n<p>person = Person(&#8220;Alice&#8221;, 30)<br \/>\nprint(repr(person))<br \/>\n&#8220;`<\/p>\n<p>\u57f7\u884c\u7d50\u679c\uff1a<\/p>\n<p>&#8220;`<br \/>\nPerson(name=&#8217;Alice&#8217;, age=30)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u500b\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u901a\u904e\u81ea\u5b9a\u7fa9 `__repr__` \u65b9\u6cd5\u4f86\u6539\u5584\u5c0d\u8c61\u7684\u8f38\u51fa\uff0c\u4f7f\u5176\u66f4\u5177\u53ef\u8b80\u6027\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u63d0\u793a<\/h2>\n<p>&#8211; **\u8f38\u51fa\u4e0d\u662f\u9810\u671f\u7684\u683c\u5f0f**\uff1a\u6aa2\u67e5 `__repr__` \u65b9\u6cd5\u662f\u5426\u6b63\u78ba\u5be6\u73fe\uff0c\u78ba\u4fdd\u8fd4\u56de\u7684\u5b57\u4e32\u7b26\u5408\u4f60\u7684\u9700\u6c42\u3002<br \/>\n&#8211; **\u7121\u6cd5\u5e8f\u5217\u5316\u67d0\u4e9b\u5c0d\u8c61**\uff1a\u78ba\u4fdd\u5c0d\u8c61\u5167\u90e8\u7684\u6240\u6709\u5c6c\u6027\u90fd\u80fd\u88ab\u6b63\u78ba\u5e8f\u5217\u5316\u70ba\u5b57\u4e32\uff0c\u5426\u5247\u53ef\u80fd\u6703\u5c0e\u81f4\u932f\u8aa4\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Python \u7684 `repr()` \u51fd\u6578\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u7528\u65bc\u7372\u53d6\u5c0d\u8c61\u7684\u5b57\u4e32\u8868\u793a\u3002\u5b83\u5728\u8abf\u8a66\u548c\u5c0d\u8c61\u5e8f\u5217\u5316\u4e2d\u767c\u63ee\u8457\u91cd\u8981\u4f5c\u7528\u3002\u638c\u63e1 `repr()` \u7684\u4f7f\u7528\u5c07\u4f7f\u4f60\u7684 Python \u7de8\u7a0b\u66f4\u70ba\u9ad8\u6548\u3002<\/p>\n<p>\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u95dc\u65bc Python \u7684\u6559\u5b78\uff0c\u53ef\u4ee5\u53c3\u8003 [\u9019\u88e1](https:\/\/vocus.cc) \u7684\u76f8\u95dc\u6587\u7ae0\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()` \u51fd\u6578\u65e8\u5728\u8fd4\u56de\u5c0d\u8c61\u7684\u5b98\u65b9\u5b57\u4e32\u8868\u793a\uff0c\u901a\u5e38\u7528\u65bc\u8abf\u8a66\uff0c\u800c `str()` \u4e3b\u8981\u7528\u65bc\u751f\u6210\u6613\u65bc\u95b1\u8b80\u7684\u5b57\u4e32\u8868\u793a\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u5728\u81ea\u5b9a\u7fa9\u985e\u4e2d\u4f7f\u7528 `repr()` \u51fd\u6578\uff1f**<br \/>\nA2: \u4f60\u53ef\u4ee5\u901a\u904e\u5be6\u73fe `__repr__` \u65b9\u6cd5\u4f86\u5b9a\u7fa9\u5c0d\u8c61\u7684\u5b57\u4e32\u8868\u793a\uff0c\u4f7f\u5176\u5728\u4f7f\u7528 `repr()` \u6642\u8fd4\u56de\u7279\u5b9a\u7684\u683c\u5f0f\u3002<\/p>\n<p>**Q3: `repr()` \u51fd\u6578\u53ef\u4ee5\u7528\u65bc\u54ea\u4e9b\u5c0d\u8c61\uff1f**<br \/>\nA3: `repr()` \u51fd\u6578\u53ef\u4ee5\u7528\u65bc\u6240\u6709 Python \u5c0d\u8c61\uff0c\u5305\u62ec\u5167\u5efa\u5c0d\u8c61\u548c\u81ea\u5b9a\u7fa9\u5c0d\u8c61\uff0c\u53ea\u8981\u5b83\u5011\u5177\u6709 `__repr__` \u65b9\u6cd5\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-3284","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-QY","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3284","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=3284"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3284\/revisions"}],"predecessor-version":[{"id":12879,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3284\/revisions\/12879"}],"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=3284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}